How to Fix Git Pull Into Wrong Branch

I work with git almost everyday and sometimes I do git pull into wrong branch unintentionally. I believe every programmer should know how to undo git pull because it is very easy.

Scenario

I’m working on my own branch (Branch B) which checkout from another branch (Branch A) and sometimes when I do git pull, I mistakenly do git pull from master branch.

Solution

You only need to execute 3 simple command after the git pull. Normally your git pull will throw an error about merge failed because of conflicts. The command as follows:

 git reset --hard
git pull origin <your branch name>
git reset --hard ORIG_HEAD 

Explanation

 git reset --hard 

This will clear the mess from the index file and the working tree. Then you may now git pull from the correct branch which is intended earlier:

 

 git pull origin <your branch name> 

Git pull will merge a topic branch into the current branch, which resulted in a fast-forward.

 

 git reset --hard ORIG_HEAD 

Reset the tip of the branch.

That’s all! Should you need more info on git reset, check out the git documentation here.

Leave a Reply

Your email address will not be published. Required fields are marked *