偶然发现之前某次 git commit 中message单词拼错了,作为完美强迫症患者,当然要找方法改之。

git rebase -i允许对一些commit的整理和修改。如果对最近的三条 commit (或者其中的任意一条)进行修改:

git rebase -i HEAD~3

或者

git rebase -i $head

指定某次 commit头文件,则可以对该commit 之前的commit 进行编辑。随后编辑器里出现文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pick 2d1a960 an into to the website
pick 651c530 ignore and delete public dir
pick 67547b4 reset number of list posts to 8 in zh page

# Rebase 1cd4b97..67547b4 onto 1cd4b97 (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
...

前三行列出了可以修改的 commit,每个提交前都有一个命令(默认为 pick)指定了作何修改,pick修改为reword来编辑 commit 的 message。

1
2
3
edit 2d1a960 an into to the website
pick 651c530 ignore and delete public dir
pick 67547b4 reset number of list posts to 8 in zh page

保存退出后,会自动滚回到列表中的最后一次提交,并出现以下提示语句:

1
2
3
4
5
6
7
8
Stopped at 2d1a960... an intro to the website
You can amend the commit now, with

	git commit --amend 

Once you are satisfied with your changes, run

	git rebase --continue

运行:

1
git commit --amend

第一行即是 commit 的 message

1
2
3
4
5
an intro to the website

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

修改保存,确认修改成功运行:

1
git rebase --continue

完成了修改某 commit 的 message,最后强制push到远程仓库完成本次任务。

1
git push --force

需要注意的是,对于多人协作的git 库,如果commit 已经 push,那么就将错就错,修改commit可能对开发者引发混乱。