Git 更改舊的 commit 訊息

記錄一下怎麼使用 git 指令來修改舊的 commit 訊息。

修改當前 commit 訊息

這應該是最基本的指令,透過 --amend 來修改當前的 commit 訊息。
可以一行指令來直接更新成新的 commit 內容

$ git commit --amend -m "New commit message."

或是執行下面的指令來進入 git 編輯模式更改原本的 commit 訊息

git commit --amend

修改歷史 commit 訊息

如果目前的情況是,想要修改之前某一筆 commit 的訊息,就得用 git rebase -i HEAD~N 的方式來修改了。其中的 N 是要修改的 commit 數量,所以如果要改的是之前的第四筆資料,就是 git rebase -i HEAD~4。這時會出現類似下面的畫面:
這時會將 HEAD~4HEAD 的訊息列出,就是最後四筆 commit 倒序列出來

$ git rebase -i HEAD~4
pick 873dfac Rename file name
pick f7f3f6d Change my name a bit
pick 310154e Update README formatting and add blame
pick a5f4a0d Add cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# 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

接著找到要修改的 commit 訊息後,將前方的 pick 更換成 reword,後存檔離開。

pick 873dfac Rename file name

reword f7f3f6d Change my name a bit

pick 310154e Update README formatting and add blame

reword a5f4a0d Add cat-file

然後就會依序出現被選出來修改的 commit 的編輯畫面,這時候再進行修改即可。

Change my name a bit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
.....
Add cat-file

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
.....

參考資料