如何重写第一个git commit消息?

Hen*_*rik 97 git message git-rebase git-commit revision-history

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

➜~myprojectgit :(主人) 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)
Run Code Online (Sandbox Code Playgroud)

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

➜~myprojectgit :(主人) 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
Run Code Online (Sandbox Code Playgroud)

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

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

......适当的事情.

不出所料,我上面的尝试没有成功,因为第一次提交显然没有任何提交.(当你rebase,你需要在你想要的之前引用下一个最早的提交reword,对吧?)

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

flo*_*sla 184

git rebase -i --root

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

这样,根提交也包含在内,您可以reword像任何其他提交一样.

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

注意:也可以看到这个类似的问题和答案.


for*_*rk0 12

你可以随时使用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
Run Code Online (Sandbox Code Playgroud)

  • @hced:您应该知道重写任何被认为是"已发布历史记录"的提交通常都是一个坏主意.在你的情况下,这意味着你通常不应该这样做,如果其他任何人都曾经在你的root用户提交作为祖先的提交. (3认同)
  • 我更新了代码以确保不会发生复制的提交ID的错误.现在代码甚至可以复制.**但是警告**:如果有多个初始提交(即合并两个或更多不相关的分支时),它无法正常工作 (2认同)

Dou*_*las 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
Run Code Online (Sandbox Code Playgroud)

  • 截至[git 1.7.12](https://raw.github.com/git/git/master/Documentation/RelNotes/1.7.12.txt),`git rebase -i --root`是要走的路,正如florisla所说. (3认同)