Git Keep only recent commits in history

09 Oct

Ever faced a situation to cleanup the history in git where you do not need to retain very older commits in history that you never will rollback or do any operation??

I faced this where i need only recent 700 commits in my history.

Here is the work around i did.

Let branch1 be the one which has some 5k+ commits committed years ago

Now i need branch2 with just recent 700 commits in the history, that is the 700th commit from present to past should be the initial one of the repo

I used the fast-export and import feature of git along with the linux sed command combined together for this

git fast-export branch1~700..branch1 | sed “s|refs/heads/branch1|refs/heads/branch2|” | git fast-import

  1. Use fast-export to export the recent 700 commits in branch1 branch
  2. sed – way to create a new branch called branch2 out of branch1 branch
  3. Import the exported 700 commits alone into the branch2 branch

Now you will have your desired branch2 in your local repo which you can push to github

Leave a comment

Posted by on October 9, 2014 in Git


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: