git 撤消上次提交/合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5623162/
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 10:35:56  来源:igfitidea点击:

Undo last commit/merge

gitversion-controlcommitrevert

提问by Siggy Petersen

I have messed up my git repo a little. I worked on a feature in a separate branch. After finishing the work, I switched to the master to merge it into, but my partner pushed a few files which came into conflict with mine. After the merge, the conflict and pushing the new changes, I saw that I committed the older changes of my partner too.

我有点搞砸了我的 git repo。我在一个单独的分支中开发了一个功能。完成工作后,我切换到master合并它,但我的伙伴推送了一些与我的冲突的文件。在合并、冲突和推动新变化之后,我看到我也提交了我伙伴的旧变化。

Now I want to redo this commit/merge. I tried git reset --soft HEAD^but when I wanted to push, I got this error message Merge the remote changes before pushing again.

现在我想重做这个提交/合并。我试过了, git reset --soft HEAD^但是当我想推送时,我收到了此错误消息Merge the remote changes before pushing again

Can anyone help me?

谁能帮我?

回答by Adam Dymitruk

Your feature branch will still point to your work. You will not lose those changes.

您的功能分支仍将指向您的工作。您不会丢失这些更改。

As plaes said, you can reset master back one with

正如 plaes 所说,你可以用

git reset --hard HEAD^

If you want to grab some specific files from your branch without merging, you can check them out:

如果你想在不合并的情况下从你的分支中获取一些特定的文件,你可以查看它们:

git checkout yourbranch -- file1 file2 etc

If you want some files from master from before the merge you can also check these out:

如果您想在合并之前从 master 获取一些文件,您还可以查看这些文件:

git checkout master^ -- file3 file4 etc

This is not ideal but it is what is needed sometimes. A merge /may/ mean that you reject some changes from either side in a merge. The best way to attain a proper merge is to:

这并不理想,但有时是需要的。合并 /may/ 意味着您拒绝合并中任一方的某些更改。实现适当合并的最佳方法是:

git merge --no-commit yourbranch

from master, then run the git checkout commands from above and finally commit:

从 master 开始,然后从上面运行 git checkout 命令,最后提交:

git add . -A
git commit

When you push this branch now, you will need to add the forceoption

当您现在推送此分支时,您将需要添加force选项

git push --force

or

或者

git push -f

Hope this helps.

希望这可以帮助。

回答by plaes

Hehe, you almost figured it out:

呵呵,你差点就明白了:

git reset --hard HEAD^

回答by ralphtheninja

The problem here is that you want to undo changes that you already have pushed to the central repository. If you hadn't pushed them in the first place, a git reset --hard/--soft/--mixed HEAD^ would have done the trick.

这里的问题是您想要撤消已经推送到中央存储库的更改。如果您一开始没有推送它们,那么 git reset --hard/--soft/--mixed HEAD^ 就可以解决问题。

So when you have reseted your HEAD git complains when you try to push to the origin and update the remote ref which is not an ancestor to your HEAD. Use --force:

因此,当您重置 HEAD 时,当您尝试推送到原点并更新不是 HEAD 祖先的远程引用时,git 会抱怨。动武:

git push --force origin master