为什么 git revert 会抱怨缺少 -m 选项?

发布于 2024-11-07 07:33:25 字数 286 浏览 11 评论 0原文

我正在与其他人一起开发一个项目,并且正在开发多个 GitHub 分支。有人刚刚修复了一个问题,我与他的分支合并,但后来我意识到我可以找到更好的解决方案。我想恢复我刚刚所做的提交。我尝试使用 git revert HEAD 执行此操作,但它给了我这个错误:

fatal: Commit <SHA1> is a merge but no -m option was given.

这是什么意思?当我合并并提交时,我确实使用 -m 选项来表示“与 <用户名> 合并”。

我在这里做错了什么?

I'm working on a project with other people, and there are 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:

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>".

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高冷爸爸 2024-11-14 07:33:25

默认情况下,git revert 拒绝恢复合并提交,因为这实际上意味着什么是不明确的。我认为您的 HEAD 实际上是合并提交。

如果你想恢复合并提交,你必须指定你想将哪个合并父级视为主干,即你想恢复到什么。

通常这将是第一个父级,例如,如果您在 master 上并进行了 git merge Unwanted,然后决定恢复 unwanted 的合并。第一个父级将是您的预合并 master 分支,第二个父级将是unwanted 的尖端。

在这种情况下,您可以执行以下操作:

git revert -m 1 HEAD

git cat-file -p [MERGE_COMMIT_ID] 将按顺序显示父分支。列出的第一个是 -m 1,第二个是 -m 2。

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.

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.

In this case you could do:

git revert -m 1 HEAD

git cat-file -p [MERGE_COMMIT_ID] will show the parent branches in order. The first one listed would be -m 1, the second -m 2.

沩ん囻菔务 2024-11-14 07:33:25

假设另一个人在 foo 之上创建了 bar,但您同时创建了 baz,然后合并,给出了

$ git lola
*   2582152 (HEAD, master) Merge branch 'otherguy'
|\  
| * c7256de (otherguy) bar
* | b7e7176 baz
|/  
* 9968f79 foo

注释的历史记录: git lola 是一个非标准但有用的别名。

git revert 没有骰子:

$ git revert HEAD
fatal: Commit 2582152... is a merge but no -m option was given.

Charles Bailey 像往常一样给出了出色的答案。使用 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

有效删除 bar 并生成以下历史记录

$ git lola
* e900aad (HEAD, master) Revert "Merge branch 'otherguy'"
*   2582152 Merge branch 'otherguy'
|\  
| * c7256de (otherguy) bar
* | b7e7176 baz
|/  
* 9968f79 foo

但我怀疑您想丢弃合并提交:

$ git reset --hard HEAD^
HEAD is now at b7e7176 baz

$ git lola
* b7e7176 (HEAD, master) baz
| * c7256de (otherguy) bar
|/  
* 9968f79 foo

如文档中所述git rev-parse手册

^,例如 HEAD^、v1.5.1^0
修订参数的后缀 ^ 表示该提交对象的第一个父对象。 ^ 表示第 n 个父级(ie ^ 相当于 <代码>^1)。作为特殊规则,^0 表示提交本身,当 是引用提交的标记对象的对象名称时使用对象。

因此在调用 git reset 之前,HEAD^(或 HEAD^1)是 b7e7176,HEAD^2 是 c7256de ,ie,分别是合并提交的第一个和第二个父级。

请小心使用git reset --hard,因为它可能会破坏工作。

Say the other guy created bar on top of foo, but you created baz in the meantime and then merged, giving a history of

$ git lola
*   2582152 (HEAD, master) Merge branch 'otherguy'
|\  
| * c7256de (otherguy) bar
* | b7e7176 baz
|/  
* 9968f79 foo

Note: git lola is a non-standard but useful alias.

No dice with git revert:

$ git revert HEAD
fatal: Commit 2582152... is a merge but no -m option was given.

Charles Bailey gave an excellent answer as usual. Using git revert as in

$ 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

$ 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 away the 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

<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.

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.

Be careful with git reset --hard because it can destroy work.

桃扇骨 2024-11-14 07:33:25

我遇到了这个问题,解决方案是查看提交图(使用 gitk)并看到我有以下内容:

*   commit I want to cherry-pick (x)
|\  
| * branch I want to cherry-pick to (y)
* | 
|/  
* common parent (x)

我现在明白我想要这样做

git cherry-pick -m 2 mycommitsha

这是因为 -m 1 会基于合并在公共父级上, -m 2 基于分支 y 合并,这就是我想要挑选的分支。

I had this problem, the solution was to look at the commit graph (using gitk) and see that I had the following:

*   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.

孤独岁月 2024-11-14 07:33:25

在最初的问题中,git 错误的“-m 选项”可能被错误地表示提交消息。正如其他答案中所解释的,使用“-m n”指定合并的第 n 个父级,以选择您认为是主干的父级。

In the original question, the '-m option' of the git error was probably mistaken to denote a commit message. As explained in the other answers, use '-m n' to specify the nth parent of the merge to pick the parent you consider to be the main trunk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文