git 推送后恢复合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12534312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:39:07  来源:igfitidea点击:

Revert a merge after being pushed

gitgit-revert

提问by Bijendra

Steps i performed:

我执行的步骤:

I have two branches branch1 and branch2,

我有两个分支 branch1 和 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?

现在我意识到在 step1 时,自动合并删除了一些代码并推送了更改代码,现在我想回到我的初始状态以恢复任何更改。寻找一些即时帮助?

回答by aleroot

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.

您可以按照官方指南恢复合并,但是这让 Git 错误地认为合并的提交仍在目标分支上。

Basically you have to :

基本上你必须:

git revert -m 1 (Commit id of the merge commit)

回答by Ilya Ivanov

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.

尝试使用git reflog <branch>找出您的分支在合并之前的位置并git reset --hard <commit number>恢复旧版本。

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

Reflog 将向您显示分支的旧状态,因此您可以将其返回到您喜欢的任何更改集。

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

确保在使用 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.

要更改远程存储库历史记录,您可以执行git push -f,但是不建议这样做,因为有人可能已经下载了由您推送的更改。

回答by Asanka sanjaya

The first option is the use of git revert.

第一个选项是使用git revert.

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

The git revertwill 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 revert将恢复的变化,但将保持历史。因此,您将无法继续在同一分支中工作,因为您再也看不到合并分支和功能分支之间的实际差异。也可以使用以下方式删除历史记录。当且仅当您是目前唯一一个将更改推送到分支的人时,请非常小心地执行此操作。

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