【问题标题】:How do I reword the very first git commit message?如何改写第一个 git commit 消息?
【发布时间】:2021-05-15 15:24:05
【问题描述】:

我有一个包含 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)

现在我想reword我的第一次提交的提交信息(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

在这种情况下,我希望更正(reword 在 git 用语中)有问题的提交消息:

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

…适当的东西。

不出所料,我上面的尝试没有成功,因为第一次提交显然没有任何 parent 提交。 (当您rebase 时,您需要将下一个最旧的提交之前引用到您希望reword 的提交,对吧?)

因此,我的问题的要点是,您可以通过其他任何方式来实现吗?

【问题讨论】:

标签: git message git-rebase git-commit revision-history


【解决方案1】:

git rebase -i --root

(指向root,而不是指向特定的提交)

这样,第一个提交也包括在内,您可以像任何其他提交一样 reword 它。

--root 选项是在 Git v1.7.12 (2012) 中引入的。在此之前,唯一的选择是使用filter-branch--amend,这通常更难做到。

注意:另见this similar question and answer

【讨论】:

    【解决方案2】:

    pcreux's gist 有一个很好的方法来改写第一次提交:

    # 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
    

    【讨论】:

    • 按照 florisla 的建议,从 git 1.7.12 开始,git rebase -i --root 是正确的选择。
    【解决方案3】:

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

    【讨论】:

    • fork0:太好了,谢谢。好奇,这是否被认为是“合法”的做法,因为缺乏更好的词。我的意思是,这样做是否常见/推荐这样做?此外,在提交消息错误的情况下,你能一次又一次地这样做吗?问这个问题的原因是因为我首先使用错误的提交 SHA-1 进行了操作,复制了您的 sn-p(您的提交是最新的提交,而我想更改第一个提交)。再次使用该命令后,这一次使用正确的 SHA-1(第一次提交;6707a66),它向我吐了。
    • 嗯,这很常见 :) 是的,你可以重复它。如果您只添加-f,它将继续并始终重写给定分支的提交。在您运行命令之前,第一次的分支引用值已保存在 refs/original/master 中。
    • 当然,您可以删除(或重命名)已保存的引用。
    • 我更新了代码以确保不会发生复制提交 ID 的错误。现在代码甚至可以复制粘贴。 警告:如果有多个初始提交(即当您合并两个或多个不相关的分支时),它将无法正常工作
    • @hced:你应该知道重写任何被认为是“已发布历史”的提交通常是一个坏主意。在您的情况下,这意味着如果其他人一直在处理以您的根提交作为祖先的提交,那么您通常不应该这样做。
    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多