Git : Making the main branch identical to other branch

Problem : Some how your main branch got polluted (few bad commits) and you want to fix it ?

Solution : You can create a single commit that will make the current branch identical in contents (and implicitly in sync) with another branch.

Explanation : Roll back your branch to the commit you feel it was working fine and create/spun another branch off from there. Now cherry pick can be done for few good commits and new branch can be tested. Once that is fine, follow below commands which will eventually create a big merge commit to make your main (polluted branch) identical to fixed branch. The merge commit will remove all un-necessary files.

$ git reset --hard <another branch>
$ git reset --soft HEAD@{1}
$ git commit

Note : Please make sure that default rebase option is configured to false
command : git config –edit

The first (hard) reset grabs the contents of the other branch into your working directory. The second (soft) reset puts your commit pointer back at the tip of your original branch, but doesn’t change the files in your index at all, thus leaving them as they were in the other branch. You can then commit that state on top of your current branch’s latest commit.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s