Re-doing a reverted merge in Git
I have run into a bit of a problem here: I had a problem-specific branch 28s
in Git, that I merged in the general develop
branch. Turns out I had done it too fast, so I used git-revert to undo the merge. Now, however, the time has come to merge 28s
into develop
, but git-merge command sees the original merge, and happily announces that all is well and branches have been already merged. What do I do now? Create a 'Revert "Revert "28s -> develop"" ' commit? Doesn't seem to be a good way to do it, but I can't imagine any other at the moment.
What the tree structure looks like:
You have to "revert the revert". Depending on you how did the original revert, it may not be as easy as it sounds. Look at the official document on this topic.
---o---o---o---M---x---x---W---x---Y
/
---A---B-------------------C---D
to allow:
---o---o---o---M---x---x-------x-------*
/ /
---A---B-------------------C---D
But does it all work? Sure it does. You can revert a merge, and from a purely technical angle, git did it very naturally and had no real troubles.
It just considered it a change from "state before merge" to "state after merge", and that was it.
Nothing complicated, nothing odd, nothing really dangerous. Git will do it without even thinking about it.So from a technical angle, there's nothing wrong with reverting a merge, but from a workflow angle it's something that you generally should try to avoid.
If at all possible, for example, if you find a problem that got merged into the main tree, rather than revert the merge, try really hard to:
- bisect the problem down into the branch you merged, and just fix it,
- or try to revert the individual commit that caused it.
Yes, it's more complex, and no, it's not always going to work (sometimes the answer is: "oops, I really shouldn't have merged it, because it wasn't ready yet, and I really need to undo all of the merge"). So then you really should revert the merge, but when you want to re-do the merge, you now need to do it by reverting the revert.
Let's assume you have such history
---o---o---o---M---W---x-------x-------*
/
---A---B
Where A, B failed commits and W - is revert of M
So before I start fixing found problems I do cherry-pick of W commit to my branch
git cherry-pick -x W
Then I revert W commit on my branch
git revert W
After I can continue fixing.
The final history could look like:
---o---o---o---M---W---x-------x-------*
/ /
---A---B---W---W`----------C---D
When I send a PR it will clearly shows that PR is undo revert and adds some new commits.
To revert the revert without screwing up your workflow too much:
- Create a local trash copy of develop
- Revert the revert commit on the local copy of develop
- Merge that copy into your feature branch, and push your feature branch to your git server.
Your feature branch should now be able to be merged as normal when you're ready for it. The only downside here is that you'll a have a few extra merge/revert commits in your history.