Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert a merge after being pushed

Tags:

git

git-revert

Steps i performed:

I have two branches branch1 and branch2,

$git branch --Initial state $branch1  $git checkout branch2 $git pull origin branch1 --Step1 

I resolve the conflicts and did a

$git commit -m "Merge resolved" 

then

$git checkout branch1 $git merge branch2 $git push origin branch1 

Now i realised that while being at step1, the auto merging removed some code and the change code was pushed, now i want to go back to my initial state in order to revert any changes.looking for some immediate help?

like image 210
Bijendra Avatar asked Sep 21 '12 16:09

Bijendra


People also ask

How do I revert a merge that has been pushed?

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.

Can merge be reverted?

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.

Can git merge be reversed?

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

How do you undo a merge after 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.


2 Answers

You can revert the merge following the official guide, however this leaves Git with the erroneous belief that the merged commits are still on the target branch.

Basically you have to :

git revert -m 1 (Commit id of the merge commit) 
like image 139
aleroot Avatar answered Sep 20 '22 14:09

aleroot


Try using git reflog <branch> to find out where your branch was before the merge and git reset --hard <commit number> to restore the old revision.

Reflog will show you older states of the branch, so you can return it to any change set you like.

Make sure you are in correct branch when you use git reset

To change remote repository history, you can do git push -f, however this is not recommended because someone can alredy have downloaded changes, pushed by you.

like image 31
Ilya Ivanov Avatar answered Sep 20 '22 14:09

Ilya Ivanov