Revert a merge after being pushed

空扰寡人 提交于 2019-12-03 01:48:31

问题


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?


回答1:


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)



回答2:


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.




回答3:


The first option is the use of git revert.

git revert -m 1 [sha-commit-before-merge]

The git revert will revert the changes but will keep the history. Therefore you will not be able to continue working in the same branch since you cannot see the actual difference between the merged branch and your feature branch anymore. Use the following way to remove history as well. Do this very carefully if and only if you are the only one pushing changes to the branch at the moment.

git reset --hard [sha-commit-before-merge]
git push [origin] [branch] --force


来源:https://stackoverflow.com/questions/12534312/revert-a-merge-after-being-pushed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!