Git如何批量修改commit message

Git如何批量修改commit message

当你好不容易攒了十几个 commit 了,然后发现 push 上去的时候因为 commit 消息格式被卡。

如果只是一条 commit message 消息格式不对,我们知道可以使用

1
git commit --amend

命令来修改。可是如果连续十几条消息错了甚至几十条消息错了,该怎么一一修改呢?别怕,git rebase 来帮你。

首先,使用 git log 数一下有多少条消息出错了,假设有9条消息出错如下:

1
2
3
4
5
6
7
8
9
10
* cc136b48 - (2 hours ago) 马赛克9 (origin/main, main, HEAD)
* 0e4e4a30 - (2 hours ago) 马赛克8 - 梅世祺
* 4469b485 - (3 hours ago) 马赛克7
* f038110b - (6 hours ago) 马赛克6
* c4746347 - (6 hours ago) 马赛克5
* edbfc586 - (10 hours ago) 马赛克4
* 7a331663 - (3 days ago) 马赛克3
* ca754c4e - (3 days ago) 马赛克2
* 6f361bb8 - (3 days ago) 马赛克1
...
1
git rebase HEAD~9 -i

运行这个命令后,我们会进入交互式编辑页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pick cc136b48 - (2 hours ago) 马赛克9 (origin/main, main, HEAD)
pick 0e4e4a30 - (2 hours ago) 马赛克8 - 梅世祺
pick 4469b485 - (3 hours ago) 马赛克7
pick f038110b - (6 hours ago) 马赛克6
pick c4746347 - (6 hours ago) 马赛克5
pick edbfc586 - (10 hours ago) 马赛克4
pick 7a331663 - (3 days ago) 马赛克3
pick ca754c4e - (3 days ago) 马赛克2
pick 6f361bb8 - (3 days ago) 马赛克1

# Rebase c4b63133..cc136b48 onto 0e4e4a30 (9 commands)
#
# 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.

我们把这些 commit 前面的 pick 全改成 e 即 edit 编辑后,保存。接着,HEAD 指针会移到这个位置:

1
2
3
4
5
6
7
8
9
10
* cc136b48 - (2 hours ago) 马赛克9 (origin/main, main)
* 0e4e4a30 - (2 hours ago) 马赛克8 - 梅世祺
* 4469b485 - (3 hours ago) 马赛克7
* f038110b - (6 hours ago) 马赛克6
* c4746347 - (6 hours ago) 马赛克5
* edbfc586 - (10 hours ago) 马赛克4
* 7a331663 - (3 days ago) 马赛克3
* ca754c4e - (3 days ago) 马赛克2
* 6f361bb8 - (3 days ago) 马赛克1 (HEAD)
...

接着 Git 会提示你

1
2
3
4
5
6
7
8
Stopped at 6f361bb8...  马赛克1
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 的 commit message,执行

1
git rebase --continue

进行下一步。 从最远的 commit 开始修改,一直到最近的 commit 直到修改成功。

当然这个方法也适用于修改一个旧 commit 的 commit message,我们只需要在 git rebase 的交互式编辑页,把想要修改的几个 commit 改成 edit,其他 commit 保持 pick 就可以了,步骤几乎一模一样。

参考资料

  1. How to Change a Git Commit Message