我如何改写第一个git commit消息?

我有一个包含3个通讯的工作树:

¶〜myproject git:(master) git log

 commit a99cce8240495de29254b5df8745e41815db5a75 Author: My Name <my@mail.com> Date: Thu Aug 16 00:59:05 2012 +0200 .gitignore edits commit 5bccda674c7ca51e849741290530a0d48efd69e8 Author: My Name <my@mail.com> Date: Mon Aug 13 01:36:39 2012 +0200 Create .gitignore file commit 6707a66191c84ec6fbf148f8f1c3e8ac83453ae3 Author: My Name <my@mail.com> Date: Mon Aug 13 01:13:05 2012 +0200 Initial commit (with a misleading message) 

现在我想重写我的第一次提交 (6707a66)的提交信息,

➜〜myproject git:(master) git rebase -i 6707

(…进入vim)

 pick 5bccda6 Create .gitignore file pick a99cce8 .gitignore edits # Rebase 6707a66..a99cce8 onto 6707a66 # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out 

在这种情况下,我想纠正(以git说法)

初始提交(带有误导消息)

…适当的东西。

不出所料,我上面的尝试没有成功,因为第一次提交显然没有任何提交。 (而且当你重新rebase ,你需要引用下一个最古老的提交之前 ,你想reword ,对不对?)

因此,我的问题的主旨可以通过其他方式来实现吗?

git rebase -i --root

(指向root而不是指向特定的提交)

这样,根提交也包括在内,您可以像任何其他提交一样对其进行reword

其他更精细的方法存在,但是大多数方法在2012年之前的“v1.7.12”中增加了“–root”选项。

你总是可以使用git filter-branch --msg-filter

 git filter-branch --msg-filter \ 'test $GIT_COMMIT = '$(git rev-list --reverse master |head -n1)' && echo "Nice message" || cat' master 

pcreux的要点有一个很好的方法来改写第一次提交:

 # You can't use rebase -i here since it takes the parent commit as argument. # You can do the following though: git checkout FIRST_COMMIT_SHA && git commit --amend && git rebase HEAD master