编辑git中的根提交?
-
22-09-2019 - |
题
有一些方法可以从以后的提交中更改消息:
git commit --amend # for the most recent commit
git rebase --interactive master~2 # but requires *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
扩展 ecdpalma的答案, ,您现在可以使用 --root
可以说出来的选项 rebase
您想重写根/第一个提交:
git rebase --interactive --root
然后,root commit将显示在rebase todo列表中,您可以选择编辑或重新命名:
reword <root commit sha> <original message>
pick <other commit sha> <message>
...
这是对 --root
从 git rebase文档 (强调我的):
重现所有可从中实现的提交
<branch>
, ,而不是用<upstream>
. 这使您可以在分支上重新列入根提交.
只是为了提供更高额定答案的替代方法:
如果您要创建回购,并且可以预先知道您将在将来重新审议其“第一个”实际提交,那么您可以通过在一开始就做出明确的空任务来完全避免此问题:
git commit --allow-empty -m "Initial commit"
然后才开始做“真实”提交。然后,您可以轻松地在该提交标准方式的基础上重新反弹 git rebase -i HEAD^
您可以使用 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
不隶属于 StackOverflow