本文翻译自: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)? 如何更改第一次提交(没有父级)的提交消息?
#1楼
参考:https://stackoom.com/question/8tNA/在Git中编辑root提交
#2楼
从Git 1.7.12开始 ,您现在可以使用
git rebase -i --root
#3楼
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: 要扩展ecdpalma的答案 ,您现在可以使用--root
选项告诉rebase
您要重写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: 然后根提交将显示在rebase TODO列表中,您可以选择编辑或重新编写它:
reword <root commit sha> <original message>
pick <other commit sha> <message>
...
This is the explanation of --root
from the Git rebase docs (emphasis mine): 这是来自Git rebase文档 (强调我的)的--root
的解释:
Rebase all commits reachable from
<branch>
, instead of limiting them with an<upstream>
. 重新引用从<branch>
可到达的所有提交,而不是用<upstream>
限制它们。 This allows you to rebase the root commit(s) on a branch . 这允许您在树枝上重新定义根提交 。
#4楼
You could use git filter-branch
: 你可以使用git filter-branch
:
cd test
git init
touch initial
git add -A
git commit -m "Initial commit"
touch a
git add -A
git commit -m "a"
touch b
git add -A
git commit -m "b"
git log
-->
8e6b49e... b
945e92a... a
72fc158... Initial commit
git filter-branch --msg-filter \
"sed \"s|^Initial commit|New initial commit|g\"" -- --all
git log
-->
c5988ea... b
e0331fd... a
51995f1... New initial commit
#5楼
Assuming that you have a clean working tree, you can do the following. 假设您有一个干净的工作树,您可以执行以下操作。
# 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
#6楼
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: 如果你正在创建一个repo,并且知道你将来会在其“第一次”实际提交之上进行变基,你可以通过在开头做一个明确的空提交来完全避免这个问题:
git commit --allow-empty -m "Initial commit"
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^
然后你可以轻松地在标准方式的基础上重新设置,例如git rebase -i HEAD^