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?
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.
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.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With