为什么 git revert 会抱怨缺少 -m 选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5970889/
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
Why does git revert complain about a missing -m option?
提问by icnhzabot
So I'm working on a project with other people, and there's multiple github forks being worked on. Someone just made a fix for a problem and I merged with his fork, but then I realized that I could find a better solution. I want to revert the commit I just made. I tried doing this with git revert HEAD
but it gave me this error:
所以我正在和其他人一起做一个项目,并且有多个 github 分支正在处理中。有人刚刚修复了一个问题,我与他的叉子合并,但后来我意识到我可以找到更好的解决方案。我想恢复我刚刚所做的提交。我尝试这样做,git revert HEAD
但它给了我这个错误:
fatal: Commit <SHA1> is a merge but no -m option was given.
What does that mean? When I merged and committed, I did use the -m option to say "Merged with <username>".
这意味着什么?当我合并并提交时,我确实使用了 -m 选项来表示“与 <username> 合并”。
What am I doing wrong here?
我在这里做错了什么?
回答by CB Bailey
By default git revert
refuses to revert a merge commit as what that actually means is ambiguous. I presume that your HEAD
is in fact a merge commit.
默认情况下git revert
拒绝恢复合并提交,因为这实际上意味着不明确。我认为你HEAD
实际上是一个合并提交。
If you want to revert the merge commit, you have to specify which parent of the merge you want to consider to be the main trunk, i.e. what you want to revert to.
如果你想恢复合并提交,你必须指定你想将合并的哪个父级视为主干,即你想要恢复到什么。
Often this will be parent number one, for example if you were on master
and did git merge unwanted
and then decided to revert the merge of unwanted
. The first parent would be your pre-merge master
branch and the second parent would be the tip of unwanted
.
通常这将是第一个父级,例如,如果您在master
并做了git merge unwanted
然后决定恢复unwanted
. 第一个父级将是您的合并前master
分支,第二个父级将是unwanted
.
In this case you could do:
在这种情况下,你可以这样做:
git revert -m 1 HEAD
回答by Greg Bacon
Say the other guy created bar on top of foo, but you created baz in the meantime and then merged, giving a history of
假设另一个人在 foo 之上创建了 bar,但您同时创建了 baz 然后合并,给出了历史
$ git lola * 2582152 (HEAD, master) Merge branch 'otherguy' |\ | * c7256de (otherguy) bar * | b7e7176 baz |/ * 9968f79 foo
Note: git lolais a non-standard but useful alias.
注意:git lola是一个非标准但有用的别名。
No dice with git revert
:
没有骰子git revert
:
$ git revert HEAD fatal: Commit 2582152... is a merge but no -m option was given.
Charles Bailey gave an excellent answeras usual. Using git revert
as in
查尔斯·贝利像往常一样给出了很好的回答。使用git revert
如
$ git revert --no-edit -m 1 HEAD [master e900aad] Revert "Merge branch 'otherguy'" 0 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 bar
effectively deletes bar
and produces a history of
有效地删除bar
并生成历史记录
$ git lola * e900aad (HEAD, master) Revert "Merge branch 'otherguy'" * 2582152 Merge branch 'otherguy' |\ | * c7256de (otherguy) bar * | b7e7176 baz |/ * 9968f79 foo
But I suspect you want to throw awaythe merge commit:
但我怀疑你想扔掉合并提交:
$ git reset --hard HEAD^ HEAD is now at b7e7176 baz $ git lola * b7e7176 (HEAD, master) baz | * c7256de (otherguy) bar |/ * 9968f79 foo
As documented in the git rev-parse
manual
如手册中所述git rev-parse
<rev>^
, e.g. HEAD^,v1.5.1^0
A suffix^
to a revision parameter means the first parent of that commit object.^<n>
means the n-th parent (i.e.<rev>^
is equivalent to<rev>^1
). As a special rule,<rev>^0
means the commit itself and is used when<rev>
is the object name of a tag object that refers to a commit object.
<rev>^
,例如 HEAD^,v1.5.1^0
修订参数的
后缀^
表示该提交对象的第一个父级。^<n>
表示第n个父级(即<rev>^
相当于<rev>^1
)。作为一个特殊规则,<rev>^0
表示提交本身,当<rev>
是引用提交对象的标记对象的对象名称时使用。
so before invoking git reset
, HEAD^
(or HEAD^1
) was b7e7176 and HEAD^2
was c7256de, i.e., respectively the first and second parents of the merge commit.
所以在调用之前git reset
,HEAD^
(或HEAD^1
)是 b7e7176 和HEAD^2
c7256de,即分别是合并提交的第一个和第二个父项。
Be careful with git reset --hard
because it can destroy work.
小心,git reset --hard
因为它会破坏工作。
回答by shmish111
I had this problem, the solution was to look at the commit graph (using gitk) and see that I had the following:
我遇到了这个问题,解决方案是查看提交图(使用 gitk)并查看我有以下内容:
* commit I want to cherry-pick (x)
|\
| * branch I want to cherry-pick to (y)
* |
|/
* common parent (x)
I understand now that I want to do
我现在明白我想做
git cherry-pick -m 2 mycommitsha
This is because -m 1
would merge based on the common parent where as -m 2
merges based on branch y, that is the one I want to cherry-pick to.
这是因为-m 1
将基于公共父级进行-m 2
合并,而基于分支 y 进行合并,这是我想要挑选的那个。