春暖花开

Git 合并多个 Commit

总是记不住 Git 合并 Commit 的命令,故今天记录一下。

1
git rebase -i HEAD~2

2 为合并的 Commit 的个数,如果合并 3 个 Commit,则改为 3,同理。

之后,可能会出现如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pick 7bb83e2 commit 1
pick ae60558 commit 2
# Rebase f59dfd0..ae60558 onto f59dfd0 (2 commands)
#
# 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
# d, drop = remove commit
#
# 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 1commit 2 为 commit 的消息,commit 1 在前,commit 2 在后。

之后,修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pick 7bb83e2 commit 1
squash ae60558 commit 2
# Rebase f59dfd0..ae60558 onto f59dfd0 (2 commands)
#
# 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
# d, drop = remove commit
#
# 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 2pick 改为 squash。然后保存退出,之后还会出现 commit 消息的修改,修改完保存退出即可。

如果前一个 commit 已经 push 到了远程仓库中,那么还需要 force push 一下:

1
git push -f
0%