在 Git 中编辑根提交?

发布于 2024-08-19 08:47:27 字数 246 浏览 8 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水波映月 2024-08-26 08:47:28

您可以使用 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

You could use 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
我三岁 2024-08-26 08:47:27

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

git rebase -i --root

文档

As of Git version 1.7.12, you may now use

git rebase -i --root

Documentation

飘过的浮云 2024-08-26 08:47:27

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

# 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

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
轮廓§ 2024-08-26 08:47:27

要扩展 ecdpalma 的答案,您现在可以使用 --root 选项来告诉 rebase 表示你要重写root/first commit:

git rebase --interactive --root

那么root commit会出现在rebase TODO列表中,你可以选择编辑或重写它:

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

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

重新设置可从 访问的所有提交,而不是使用 限制它们。 这允许您在分支上重新确定根提交

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.

春风十里 2024-08-26 08:47:27

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

如果您正在创建一个存储库,并且预先知道您将在未来的“第一次”实际提交之上进行变基,那么您可以通过明确地避免此问题开始时为空提交:

git commit --allow-empty -m "Initial commit"

然后才开始进行“真正的”提交。然后,您可以轻松地以标准方式在提交之上进行变基,例如 git rebase -i HEAD^

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:

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^

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文