如何在Git中更改先前提交之一的提交消息?

时间:2014-06-07 15:53:19

标签: git

我想更改之前提交的提交消息。我想我可以

  • 使用git reset <the commit id>
  • 返回我想修改的提交
  • 使用git commit --amend
  • 更改提交
  • 使用git reset <my last commit id>返回。

不幸的是它不起作用。在git commit --amend之后,我确实看到消息已更改,但在git reset <my last commit id>之后,我看到旧的未更改的提交消息。

如何更改提交消息?

1 个答案:

答案 0 :(得分:3)

可以使用rebase

完成此操作
  • git rebase -i <commit id>^ - 打开您$EDITOR上的提交列表。由于您需要指定要编辑的提交的,因此需要该行末尾的插入符号。
  • 将行pick <commit id>更改为reword <commit id>,保存并关闭文件。

      

    r,reword =使用提交,但编辑提交消息

  • 它将立即重新打开并加载指定的提交。编辑邮件并保存。

  • 请注意,此会更改所有后续的提交ID !与所有其他git对象一样,提交哈希值是根据其内容计算的,其中包括提交消息。

此外,如果修改已发布的提交,则应了解重写历史记录的含义。  有关rebase的详细信息,请参阅http://git-scm.com/book/en/Git-Tools-Rewriting-Historyhttps://www.kernel.org/pub/software/scm/git/docs/git-rebase.html


为什么git reset无效

恕我直言,这个问题的一部分正在研究为什么你的初步尝试没有按预期工作。基本上它归结为:提交对象是不可改变的

git reset B使分支指向特定的提交:

A -- B -- C -- D
     ^

git commit --ammend创建了一个备用时间轴:

A -- B -- C -- D
 \
  *- E
     ^

通过git reset D移回到将来,您所做的更改将被丢弃。

A -- B -- C -- D
 \             ^
  *- E
相关问题