Git:合并后如何重置?

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

Git: How to reset after merging?

gitgit-revert

提问by Jonathan

I've merged a master branch from a friend's repository into my working directory into branch_a using:

我使用以下命令将朋友存储库中的主分支合并到我的工作目录中的 branch_a 中:

git pull my_friend master

I've discovered that the merged version has errors. To continue development I would like to revert to my last commit before the merge.
I tried:

我发现合并版本有错误。为了继续开发,我想恢复到合并前的最后一次提交。
我试过:

git reset --hard HEAD

But that brought me back to the state right after the merge. (does pull command commit?!)
I also tried:

但这让我在合并后立即回到了状态。(拉命令是否提交?!)
我也试过:

git revert HEAD

but received the following error:

但收到以下错误:

fatal: Commit 2d72d8f367b987d8c16f5cb1a543a6886acdcf83 is a merge but no -m option was given.

致命:提交 2d72d8f367b987d8c16f5cb1a543a6886acdcf83 是一个合并,但没有给出 -m 选项。

What should I do?

我该怎么办?

回答by Cascabel

HEADrefers to the current commit (generally the tip of the currently checked-out branch). You've already committed your merge, so HEADis pointing to the merge commit. If you want to get back to the commit before it, use:

HEAD指的是当前提交(通常是当前签出分支的提示)。您已经提交了合并,因此HEAD指向合并提交。如果要返回之前的提交,请使用:

git reset --hard HEAD^

The ^means "first parent of"; for a regular commit it's the only parent, and for a merge commit it's the commit you had checked out when you merged (i.e. the prior tip of the branch you merged into).

^装置“的第一个亲本”; 对于常规提交,它是唯一的父级提交,对于合并提交,它是您在合并时检出的提交(即您合并到的分支的先前提示)。

And of course, if you ever get really lost, just open up gitk, and either copy/paste the SHA1 of the commit you want to reset to (git reset --hard SHA1) or just right click on it and reset within gitk.

当然,如果您真的迷路了,只需打开gitk,然后将要重置的提交的 SHA1 复制/粘贴到 ( git reset --hard SHA1) 或右键单击它并在gitk.

By the way, revertdoesn't mean what you think it does (it sounds like you're using it in an svn way, maybe? but I've never used svn). git revertis used to create a commit which cancels out (reverts) a previous commit, by applying the reverse diff. You use it when you want to undo a single previous commit which has already been published.

顺便revert说一句,这并不意味着您认为它的作用(听起来您正在以 svn 的方式使用它,也许吧?但我从未使用过 svn)。git revert用于创建一个提交,通过应用反向差异来取消(恢复)先前的提交。当您想要撤消已经发布的单个先前提交时,您可以使用它。

回答by Ben James

After the pull, HEADhas been moved to the latest commit, which is the merge commit bringing the two branches together, not the commit that you last worked on. So this is why reset HEADwon't change anything.

在 pull 之后,HEAD已移至最新提交,这是将两个分支合并在一起的合并提交,而不是您上次处理的提交。所以这就是为什么reset HEAD不会改变任何东西。

revert HEADisn't what you want to do either, because that is attempting to create a new commit reverting the changes of the merge commit. (This is why you see an error; you can't revert a merge commit without telling git which of the two parent commits you want to return to.)

revert HEAD也不是您想要做的,因为那是尝试创建一个新提交来恢复合并提交的更改。(这就是你看到错误的原因;你不能在不告诉 git 你想要返回到两个父提交中的哪一个的情况下恢复合并提交。)

I think what you want is:

我想你想要的是:

git reset [--hard] HEAD^

which will reset to the commit beforethe merge commit.

这将重置为合并提交之前的提交。

回答by Lakshman Prasad

does pull command commit?

pull 命令会提交吗?

Yes, it does. Pull is a wrapper over fetchand merge. If you want to see other's changes before merging, you can just fetch

是的,它确实。Pull 是对fetch和的包装merge。如果你想在合并之前看到其他人的变化,你可以fetch