我如何改写第一个 git commit 消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11987914/
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-19 07:21:20  来源:igfitidea点击:

How do I reword the very first git commit message?

gitmessagegit-rebasegit-commitrevision-history

提问by Henrik

I have a working tree containing 3 commmits:

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

? ~myproject git:(master) git log

? ~myproject git:(大师)git log

commit a99cce8240495de29254b5df8745e41815db5a75
Author: My Name <[email protected]>
Date:   Thu Aug 16 00:59:05 2012 +0200

    .gitignore edits

commit 5bccda674c7ca51e849741290530a0d48efd69e8
Author: My Name <[email protected]>
Date:   Mon Aug 13 01:36:39 2012 +0200

    Create .gitignore file

commit 6707a66191c84ec6fbf148f8f1c3e8ac83453ae3
Author: My Name <[email protected]>
Date:   Mon Aug 13 01:13:05 2012 +0200

    Initial commit (with a misleading message)

Now I wish to rewordthe commit message of my first commit(6707a66)

现在我希望reword第一次提交的提交消息(6707a66)

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

? ~myproject git:(大师)git rebase -i 6707

(…entering vim)

(……进入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

In this case, I wish to correct (rewordin git parlance) the commit message in question:

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

Initial commit (with a misleading message)

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

…to something appropriate.

……适合的东西。

Unsurprisingly, my attempt above didn't succeed since the first commit obviously doesn't have any parentcommit. (And when you rebase, you need to reference the next oldest commit priorto the one you wish to reword, right?)

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

The gist of my question, thus, can you achieve this by any other means of doing it?

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

回答by florisla

Do git rebase -i --root

git rebase -i --root

(point to rootinstead of pointing to a specific commit)

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

This way, the first commit is also included and you can just rewordit like any other commit.

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

The --rootoption was introduced in Git v1.7.12(2012). Before then the only option was to use filter-branchor --amend, which is typically harder to do.

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

Note: see also this similar question and answer.

注意:另请参阅此类似问答

回答by fork0

You can always use git filter-branch --msg-filter:

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

回答by Douglas

pcreux's gisthas a good way to reword the first commit:

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