问题描述
我已经将一个朋友的存储库中的主分支合并到了我的工作目录中,使用:
git pull my_friend master
我发现合并版本有错误。为了继续开发,我想恢复到合并前的最后一次提交。
我试过了:
git reset --hard HEAD
但是这使我在合并后立即回到了状态。 (不拉命令提交?!)
我也试过:
git revert HEAD
,然后复制/粘贴要重置的提交的SHA1(
但收到以下错误:
fatal:提交
2d72d8f367b987d8c16f5cb1a543a6886acdcf83
是一个合并,但没有给出-m选项。
什么我应该怎么做?
解决方案
HEAD
当前签出的分支的尖端)。您已经提交了合并,因此HEAD
指向合并提交。如果你想回到之前的提交,请使用:
git reset --hard HEAD ^
^
表示第一父母;对于常规提交,它是唯一的父对象,对于合并提交,它是您合并时检出的提交(即,您合并到的分支的先前提示)。
gitkgit reset --hard SHA1
)或者右键单击它并在gitk
中重置。
顺便说一句,
revert
并不意味着你认为它的确如此(听起来你正在以svn的方式使用它,也许?但我从来没有使用svn)。git revert
用于创建一个提交,通过应用reverse diff来取消(恢复)前一个提交。当你想撤消一个已经发布的提交时,你可以使用它。I've merged a master branch from a friend's repository into my working directory into branch_a using:
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:
What should I do?
解决方案
HEAD
refers to the current commit (generally the tip of the currently checked-out branch). You've already committed your merge, soHEAD
is pointing to the merge commit. If you want to get back to the commit before it, use: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 withingitk
.By the way,
revert
doesn'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 revert
is 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.这篇关于Git:合并后如何重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!