Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert changes made by merge

Tags:

git

revert

The developer was commiting small changes to two files. But during this commit, he had a merge conflict which deleted a lot of stuff (probably didn't have the last up to date version). Then it was pushed to the shared repo and some other developers did some other commits.

Now, we noticed, that the merge deleted the important files, and we want to revert it back.
How can I do this without losing the changes from the next commits?

I was trying to git revert commitsha, but it didn't bring the changes back. Do I need to revert back the mergesha? How can I determine it?

like image 970
Sfisioza Avatar asked Nov 30 '11 08:11

Sfisioza


People also ask

Can merge be reverted?

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.

How do I revert changes from merged to master?

simply run git reset --hard to revert all those changes.

What happens when you revert a merge?

If the merge of master to the feature branch was unintentional. The correct way to undo it is to reset the branch. This can be done by running the following in the feature branch. On the other hand, reverting a merge commit negates all the changes made by the branch of the specified parent.

How do I undo a merge conflict?

On the command line, a simple "git merge --abort" will do this for you. In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.


1 Answers

git revert --mainline

Usually:

git revert --mainline 1 dd8cbe3e4 

Where:

  • dd8cbe3e4 is the bad merge commit you want to undo, and
  • --mainline tells you which of the multiple previous commits is the one to restore (remember, a merge commit has multiple parent commits and you can only keep one of them).
    • I can't find a good explanation of what the 1 means, but my guess is that 1,2,3... corresponds to a list of mappings to the commits immediately before dd8cbe3e4, sorted by ascending chronological order (oldest first - which is usually what you want to revert to).

Source:

http://thezencoder.com/2013/09/05/how-to-correctly-revert-a-bad-merge-in-git/

like image 51
Sridhar Sarnobat Avatar answered Sep 21 '22 02:09

Sridhar Sarnobat