Edit the root commit in Git?
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)?
假设你有一个干净的工作树,你可以执行以下操作。
# checkout the root commit
git checkout <sha1-of-root>
# amend the commit
git commit --amend
# rebase all the other commits in master onto the amended root
git rebase --onto HEAD HEAD master
从Git 1.7.12版开始,你现在可以使用
git rebase -i --root
To expand on ecdpalma's answer, you can now use the --root
option to tell rebase
that you want to rewrite the root/first commit:
git rebase --interactive --root
Then the root commit will show up in the rebase TODO list, and you can select to edit or reword it:
reword <root commit sha> <original message>
pick <other commit sha> <message>
...
This is the explanation of --root
from the Git rebase docs (emphasis mine):
Rebase all commits reachable from <branch>
, instead of limiting them with an <upstream>
. This allows you to rebase the root commit(s) on a branch .
上一篇: 如何解决“腐败”的互动式底线?
下一篇: 编辑Git中的根提交?