繁体   English   中英

在 Git 中编辑根提交?

[英]Edit the root commit in Git?

有多种方法可以更改以后提交的消息:

git commit --amend                    # for the most recent commit
git rebase --interactive master~2     # but requires *parent*

如何更改第一次提交(没有父提交)的提交消息?

从 Git 版本1.7.12 开始,您现在可以使用

git rebase -i --root

文档

假设您有一个干净的工作树,您可以执行以下操作。

# 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

要扩展ecdpalma 的答案,您现在可以使用--root选项告诉rebase您要重写根/第一次提交:

git rebase --interactive --root

然后根提交将显示在 rebase TODO 列表中,您可以选择对其进行编辑或改写:

reword <root commit sha> <original message>
pick <other commit sha> <message>
...

这是Git rebase 文档--root的解释(重点是我的):

将所有可从<branch>访问的提交变基,而不是用<upstream>限制它们。 这允许您在分支上重新设置根提交

只是为了提供更高评级答案的替代方案:

如果你正在创建一个 repo,并且预先知道你将在未来的“第一次”真正提交的基础上进行 rebase,你可以通过在开始时进行明确的空提交来完全避免这个问题:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM