Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to revert a branch merge without overwriting history?

Tags:

I have two branches: master and opengl. I recently finished implementation (or at least I thought so) of opengl branch and decided to merge it into master:

git checkout master git merge opengl git push 

After I did this, several developers who are working on the master branch pulled my changes and it turned out that my implementation conflicted with some of their code. Therefore I would like to revert the merge operation on the master branch, but without overwriting history.

Note that I would like to be able to merge the opengl branch into master eventually (after I fix all the bugs). Therefore simply checking out older version of master and committing it will not work - newly created commit will cancel my changes from opengl when I will try to merge it.

Thank you.

like image 310
Sergiy Belozorov Avatar asked Mar 21 '11 20:03

Sergiy Belozorov


People also ask

How do I revert a merged branch?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

Does git merge change history?

A merge takes two or more branches, and creates a commit that interleaves the changes in all parents (resolving conflicts, as needed; the user has to decide how). git does not change history by itself.

How do you undo a git merge after pushing the changes?

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can right-click on the merge commit and select Revert commit from the context menu.

How do I undo a merge in progress?

How do I cancel a git merge? Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.


1 Answers

The documentation "How to revert a faulty merge" mentioned by cebewee explains why a git revert is tricky when reverting a merge.

So the merge will still exist, and it will still be seen as joining the two branches together, and future merges will see that merge as the last shared state - and the revert that reverted the merge brought in will not affect that at all.
If you think of "revert" as "undo", then you're going to always miss this part of reverts.
Yes, it undoes the data, but no, it doesn't undo history.

git revert is the right solution here, but it will have a consequence in the future, when you want to merge that branch again.
The next merge will then have to "revert the revert" first, and then merge the branch.

like image 147
VonC Avatar answered Sep 19 '22 06:09

VonC