I was pair programming and my partner somehow merged another pair’s branch and made several commits after. My partner never did a git merge so we were confused how that happened. But the important thing is… HOW DO WE FIX IT?!
I worried it would be very difficult and risky like doing a git rebase or something, but luckily it’s a very simple fix.
Revert a normal commit
git revert <commit hash>
Revert a merge commit
git revert <commit hash> -m 1
Note that commit hash
is the 7 character id associated with the commit you want to delete.
You will then be asked to add your commit message as you would a normal commit. Then git push
.
That’s it! You’re done!
Extra explanation for those who want it…
What is -m 1
? The -m
specifies the parent number and merge commits have more than one parent. Git doesn’t know automatically which parent branch is the main one and which one you want to un-merge.
When you do a git log
, you will see next to Merge
, the parent branches
commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: John Doe <john@example.com>
Date: Wed Oct 21 22:49:41 2020 +0100
In this situation, git revert 8f937c6 -m 1
will get you the tree as it was in 8989ee0
, and git revert -m 2
will reinstate the tree as it was in 7c6b236
.
If you merged another branch with your’s, then your branch is most likely the first one so -m 1 will be what you use.
Hopefully this has helped someone to become 1 step closer to becoming a better developer.
Top comments (0)