合并一个feature 分支上多个commit
1、 查看这个feature分支上的提交历史记录
<!-- 查看提交详情 -->
git log
<!-- 一般用下面这条简洁的 -->
git log --oneline
比如下面我从master分支切换一个test分支出来,然后做了三次修改提交。
$ git log --oneline
a3f5ef3 (HEAD -> test) 第三次修改
9e3957c 第二次修改
882a7ec 第一次修改
1f9f99c 解决冲突
a8da960 新分支再次提交
a203bc6 master分支再次编辑
dbeeda1 新分支第一次提交
ff6d8df 创建新分支
f84ebed 第四次编辑
9b06051 编辑文件夹
f2c5757 新建文件夹
查询到的commit记录是按照时间顺序排列的,前面三次 commit是test分支的提交,后面的都是master分支提交。
然后自己也可以通过souceTree 查看,树状图有可能更清晰点
2、怎么把这三个commit合并成一个
- 第一种方法,从HEAD版本开始往后数3个版本
git rebase -i HEAD~3
- 第二种方法*,找到要合并版本commit的前一个commit,那就是找到[1f9f99c 解决冲突] 这个提交,以这个为坐标,后面的提交commit都会包括进去
git rebase -i 1f9f99c
- 执行上面的命令之后,你会见到以下的窗口
pick 882a7ec 第一次修改
pick 9e3957c 第二次修改
pick a3f5ef3 第三次修改
# Rebase 1f9f99c..a3f5ef3 onto 1f9f99c (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.
看上面的命令提示,我们一般只要用到squash和fixup,看后面的英文提示可知,squash是使用这次commit message,同时把这次的commit融入到上一次commit之中,fixup和squash类似,但是会丢弃commit message. 这里我们将第二次和第三次的pick改为squash(保留这次的commit message)或者 fixup(丢弃commit message), 如下
pick 882a7ec 第一次修改
squash 9e3957c 第二次修改
squash a3f5ef3 第三次修改
# Rebase 1f9f99c..a3f5ef3 onto 1f9f99c (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.
如果是用squash 修改完后,用下面的命令操作退出
:wq
然后就会进入下面的编辑窗口
# This is a combination of 3 commits.
# This is the 1st commit message:
第一次修改
# This is the commit message #2:
第二次修改
# This is the commit message #3:
第三次修改
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Jan 13 14:48:11 2021 +0800
#
# interactive rebase in progress; onto 1f9f99c
# Last commands done (3 commands done):
# squash 9e3957c 第二次修改
# squash a3f5ef3 第三次修改
你可以再次操作:wq 退出,那么三次的commit message 就会汇集一起,你可以修改commit message,例如,我修改成【三次修改commit汇合】,然后操作:wq 退出后,会有下面的结果
$ git rebase -i HEAD~3
[detached HEAD 68d0afb] 三次修改commit汇合
Date: Wed Jan 13 14:48:11 2021 +0800
1 file changed, 7 insertions(+)
Successfully rebased and updated refs/heads/test.
- 再次查看提交记录,你会发现如下结果,前面三次commit生成了一个新的commit
$ git log --oneline
68d0afb (HEAD -> test) 三次修改commit汇合
1f9f99c 解决冲突
a8da960 新分支再次提交
a203bc6 master分支再次编辑
dbeeda1 新分支第一次提交
ff6d8df 创建新分支
f84ebed 第四次编辑
9b06051 编辑文件夹
f2c5757 新建文件夹
- 如果是使用fixup,退出后,则不会再次进入编辑commit message窗口,git会直接使用p第一次的commit message,直接返回一下信息
$ git rebase -i HEAD~3
Successfully rebased and updated refs/heads/test.
合并途中其他情况
- 如果操作到一半不想合并了,或者产生冲突了,那你通过:wq 退出后,需要操作下面的命令放弃这次压缩
git rebase --abort