Skip to main content

Git合并多次commit

1. 运行 git log

查看当前要合并的前一个提交commit id

commit 725203d03084f7c7f2993904dcc91a8d1645e54f (HEAD -> master)
Author: leon <582104384@qq.com>
Date: Thu Nov 17 20:56:20 2022 +0800

feat: 第3次提交

commit dda5b40de006650171d0a206ce0f8db90e2edc13
Author: leon <582104384@qq.com>
Date: Thu Nov 17 20:56:11 2022 +0800

feat: 第2次提交

commit 130ff18aa44bf4a3603941abeda6158f229e009d
Author: leon <582104384@qq.com>
Date: Thu Nov 17 20:55:55 2022 +0800

feat: 第1次提交

commit fc326a9bc9527219a63f1f9159c494fd5441f270
Author: leon <582104384@qq.com>
Date: Thu Nov 17 20:45:56 2022 +0800

2. git rebase -i [合并commit段之外的前一个commit id]

合并第1、2、3次提交,运行以下命令:

git rebase -i fc326a9bc9527219a63f1f9159c494fd5441f270

CMD输出:

pick 130ff18 feat: 第1次提交
pick dda5b40 feat: 第2次提交
pick 725203d feat: 第3次提交

# Rebase fc326a9..725203d onto fc326a9 (3 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.
#

修改如下:

pick 130ff18 feat: 第1次提交
s dda5b40 feat: 第2次提交
s 725203d feat: 第3次提交

含义是:保留 第1次提交 commit,第2次提交第3次提交合并到第1次提交

注意

第一个commit不可以修改为squash,因为它已经是第1个commit,没有previous commit可以合并

CMD输入:wq,保存退出。

3. 运行git status查看状态

ast commands done (3 commands done):
squash dda5b40 feat: 第2次提交
squash 725203d feat: 第3次提交
(see more in file .git/rebase-merge/done)
No commands remaining.
You are currently rebasing branch 'master' on 'fc326a9'.
(all conflicts fixed: run "git rebase --continue")

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md

4. 运行git rebase --continue

弹出以下信息:

# This is a combination of 3 commits.
# This is the 1st commit message:

feat: 第1次提交

# This is the commit message #2:

feat: 第2次提交

# This is the commit message #3:

feat: 第3次提交

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Nov 17 20:55:55 2022 +0800
#
# interactive rebase in progress; onto fc326a9
# Last commands done (3 commands done):
# squash dda5b40 feat: 第2次提交
# squash 725203d feat: 第3次提交
# No commands remaining.
# You are currently rebasing branch 'master' on 'fc326a9'.
#
# No changes

对合并后的commit信息进行修改如下:

# This is a combination of 3 commits.
# This is the 1st commit message:

feat: 合并了第1,2,3次提交


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Thu Nov 17 20:55:55 2022 +0800
#
# interactive rebase in progress; onto fc326a9
# Last commands done (3 commands done):
# squash dda5b40 feat: 第2次提交
# squash 725203d feat: 第3次提交
# No commands remaining.
# You are currently rebasing branch 'master' on 'fc326a9'.
#
# No changes

CMD输入:wq,保存退出。

CMD提示信息如下:

[detached HEAD 0f4df10] feat: 合并了第1,2,3次提交
Date: Thu Nov 17 20:55:55 2022 +0800
Successfully rebased and updated refs/heads/master.

信息表示合并提交成功。

5. 运行 git log 查看结果

commit 0f4df10d0adfc9ddf55af95f427317940dc96549 (HEAD -> master)
Author: leon <582104384@qq.com>
Date: Thu Nov 17 20:55:55 2022 +0800

feat: 合并了第1,2,3次提交

commit fc326a9bc9527219a63f1f9159c494fd5441f270
Author: leon <582104384@qq.com>
Date: Thu Nov 17 20:45:56 2022 +0800

结果显示:已合并成功。