Как переписать первое сообщение об объявлении git?

У меня есть рабочее дерево, содержащее 3 сообщения:

➜ ~ myproject git:( master) 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)

Теперь я хочу reword сообщение о совершении моего первого коммита (6707a66)

➜ ~ myproject git:( мастер) 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 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 in git parlance) сообщение о комманде:

Первоначальная фиксация (с вводящим в заблуждение сообщением)

... к чему-то соответствующему.

Неудивительно, что моя попытка выше не удалась, так как первая фиксация явно не имеет какого-либо родительского коммита. (И когда вы rebase, вам нужно ссылаться на следующую старую фиксацию до того, который вы хотите reword, правильно?)

Суть моего вопроса, таким образом, вы можете достичь этого любыми другими способами этого?

Ответ 1

Делать git rebase -i --root

(указать на root вместо того, чтобы указывать на конкретный коммит)

Таким образом, первый коммит также включается, и вы можете просто reword сделать его, как и любой другой коммит.

Опция --root была представлена в Git v1.7.12 (2012). До этого единственным вариантом было использование filter-branch или amend, что обычно сложнее сделать.

Примечание: см. также этот похожий вопрос и ответ.

Ответ 2

Вы всегда можете использовать 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

Ответ 3

pcreux 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