在 Git 中编辑根提交?
有多种方法可以更改以后提交的消息:
git commit --amend # for the most recent commit
git rebase --interactive master~2 # but requires *parent*
如何更改第一个提交(没有父级)的提交消息?
There's ways to change the message from later commits:
git commit --amend # for the most recent commit
git rebase --interactive master~2 # but requires *parent*
How can you change the commit message of the very first commit (which has no parent)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 git filter-branch :
You could use
git filter-branch
:从 Git 版本 1.7.12 开始,您现在可以使用
文档
As of Git version 1.7.12, you may now use
Documentation
假设您有一个干净的工作树,您可以执行以下操作。
Assuming that you have a clean working tree, you can do the following.
要扩展 ecdpalma 的答案,您现在可以使用
--root
选项来告诉rebase
表示你要重写root/first commit:那么root commit会出现在rebase TODO列表中,你可以选择编辑或重写它:
这是
的解释-- root
来自Git rebase 文档(强调我的):To expand on ecdpalma's answer, you can now use the
--root
option to tellrebase
that you want to rewrite the root/first commit:Then the root commit will show up in the rebase TODO list, and you can select to edit or reword it:
This is the explanation of
--root
from the Git rebase docs (emphasis mine):只是为了提供更高评价的答案的替代方案:
如果您正在创建一个存储库,并且预先知道您将在未来的“第一次”实际提交之上进行变基,那么您可以通过明确地避免此问题开始时为空提交:
然后才开始进行“真正的”提交。然后,您可以轻松地以标准方式在提交之上进行变基,例如 git rebase -i HEAD^
Just to provide an alternative to the higher rated answers:
If you are creating a repo, and know upfront that you'll be rebasing on top of its "first" real commit in the future, you can avoid this problem altogether by making an explicit empty commit at the beginning:
and only then start doing "real" commits. Then you can easily rebase on top of that commit the standard way, for example
git rebase -i HEAD^