如何重新编辑第一次提交的 Git 提交信息?

151

我有一个包含3个提交的工作树:

➜ ~myproject git:(master) git log

commit a99cce8240495de29254b5df8745e41815db5a75
Author: My Name <my@mail.com>
Date:   Thu Aug 16 00:59:05 2012 +0200

    .gitignore edits

commit 5bccda674c7ca51e849741290530a0d48efd69e8
Author: My Name <my@mail.com>
Date:   Mon Aug 13 01:36:39 2012 +0200

    Create .gitignore file

commit 6707a66191c84ec6fbf148f8f1c3e8ac83453ae3
Author: My Name <my@mail.com>
Date:   Mon Aug 13 01:13:05 2012 +0200

    Initial commit (with a misleading message)

现在我想要修改我的第一次提交(6707a66)的提交信息

➜ ~myproject git:(master) git rebase -i 6707

(...进入vim)

pick 5bccda6 Create .gitignore file
pick a99cce8 .gitignore edits

# Rebase 6707a66..a99cce8 onto 6707a66
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
在这种情况下,我希望更正(在git术语中称为reword)问题中的提交信息:
  

初始提交(具有误导性的消息)

...变成合适的内容。

毫不奇怪,我的尝试以上并没有成功,因为第一次提交显然没有任何提交。(当你rebase时,你需要引用比你想要reword的提交早的下一个提交,对吗?)

因此,我的问题的要点是:你是否可以通过其他方式来实现这个目标?


或者你可以将它永久地保留作为代码库,这是一个不太正常的细节。(http://www.youtube.com/watch?v=1A4JfCmu5oc#t=0m50s) - Christopher
4
可能是 Changing the message of the first commit? (git) 的重复问题。 - Mark Longair
1
@hced: :) 您的文案不会被浪费 - 即使它因为重复问题而关闭,它仍将帮助其他人找到解决方案。 - Mark Longair
2
任何遇到这个问题的人都可能会发现我的答案对于更改第一次提交的消息?(git)非常有帮助。链接在此:我的答案 - user456814
1
可能是 Git 中编辑根提交? 的重复问题。 - underscore_d
显示剩余2条评论
3个回答

296

执行 git rebase -i --root

(指向根节点,而不是指向特定的提交)

这样做可以包含第一个提交,而且您可以像处理其他提交一样使用 reword 命令对其进行修改。

选项 --root 是在 Git v1.7.12(2012 年)引入的。在那之前,唯一的选项是使用 filter-branch--amend,通常更难执行。

注意:请参阅此类似问题和答案


1
2022年仍然是英雄 - Kate Donaldson

12

pcreux的代码片段提供了一种很好的方法来改写第一个提交:

# You can't use rebase -i here since it takes the parent commit as argument.
# You can do the following though:
git checkout FIRST_COMMIT_SHA && git commit --amend && git rebase HEAD master

3
git 1.7.12 开始,建议使用 git rebase -i --root,正如 florisla 所建议的那样。 - Douglas

11

你可以始终使用 git filter-branch --msg-filter

git filter-branch --msg-filter \
  'test $GIT_COMMIT = '$(git rev-list --reverse master |head -n1)' &&
echo "Nice message" || cat' master

1
fork0:太好了,谢谢。我很好奇,这是否被认为是“合法”的做法,缺乏更好的词语。我的意思是,这样做是否常见/推荐?此外,在提交消息有误的情况下,您是否可以一次又一次地执行此操作?之所以问这个问题,是因为我最初使用了错误的提交SHA-1,复制了您的片段(您的是最新的提交,而我想更改第一个提交)。在再次使用命令时,这次使用正确的SHA-1(第一次提交;6707a66),它对我进行了拒绝。 - Henrik
好的,这是很常见的 :) 是的,你可以重复它。如果你只是加上 -f,它就会继续重写给定分支的提交记录。第一次的分支参考值保存在运行命令之前的 refs/original/master 中。 - fork0
当然,您可以直接删除(或重命名)保存的引用。 - fork0
2
我更新了代码,确保不会发生复制提交ID的错误。现在代码甚至可以复制粘贴。但是需要注意的是:如果存在多个初始提交(即当您合并两个或更多不相关的分支时),它将无法正常工作。 - fork0
3
你应该意识到,重新编写任何被认为是“已发布历史记录”的提交通常都不是一个好主意。在你的情况下,这意味着如果有其他人曾经在以你的根提交为祖先的提交上进行工作,你通常不应该这样做。 - Mark Longair

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接