DEV Community

git revert, merge commits, confusion

Gabriel Guzman on September 08, 2017

I ran into a gap in my understanding of git and merge commits yesterday. Most of the time, the projects I contribute to work with a "mergeless" c...
Collapse
 
4nduril profile image
Tobias Barth • Edited

There is a nice explanation here: git-scm.com/book/en/v2/Git-Tools-A... Look for the "Undoing merges" section further down.

If you revert a merge commit and later on you want to "re-merge" that same branch, you should revert the earlier revert commit (the commit that reverted the first merge) which effectively restores that merge. And if you have new commits in the feature branch since you merged for the first time you'll have to git merge <feature> again.

So:
You create a merge: git merge <feature>
You revert that (creating a new commit, say, <revmerge>): git revert -m 1 <mergecommit>

Later, you revert the revert: git revert <revmerge>
And merge in any changes to after the initial merge: git merge <feature>

Collapse
 
gabeguz profile image
Gabriel Guzman

Ah, this is the piece of documentation I was missing on Thursday, thanks for that!

Collapse
 
sanedragon profile image
Dag Arneson

This is esoteric but easy to resolve: start your new branch where you fix the merge commit with a revert of the revert of the merge commit.

Collapse
 
gabeguz profile image
Gabriel Guzman

Thanks for the tip Dag!

Collapse
 
jaysonesmith profile image
Jayson Smith

Git can be so confusing sometimes. :(

Collapse
 
pstorch profile image
Peter Storch

I would move the pointer for master back to commit a:
git branch -f master 4

Collapse
 
gabeguz profile image
Gabriel Guzman

I'm not sure I follow exactly, but I'll play with this and see how it goes. Thanks!