在 Git 中编辑根提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2119480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:42:15  来源:igfitidea点击:

Edit the root commit in Git?

gitgit-rebasegit-commitgit-rewrite-historyamend

提问by 13ren

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)?

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

采纳答案by CB Bailey

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

回答by ecdpalma

As of Git version 1.7.12, you may now use

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

git rebase -i --root

Documentation

文档

回答by CB Bailey

To expand on ecdpalma's answer, you can now use the --rootoption to tell rebasethat you want to rewrite the root/first commit:

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

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 --rootfrom the Git rebase docs(emphasis mine):

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

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.

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

回答by jakub.g

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,并且预先知道你将在未来的“第一次”真正提交的基础上进行 rebase,你可以通过在开始时进行明确的空提交来完全避免这个问题:

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^

回答by Alexander Gro?

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