git 合并多个commit

当你提交代码进行代码审查时或者创建一次pull request (这在开源项目中经常发生),你的代码在被接受之前会被要求做一些变更。于是你进行了变更,并且直到下一次审查之前你没有再次被要求进行变更过。在你知道又要进行变更之前,你已经有了一些额外的commit。理想情况下,你可以用rebase命令把多个commit压缩成一个。

git rebase -i HEAD~[number_of_commits]

如果你想要压缩最后两个commit,你需要运行下列命令。

git rebase -i HEAD~3

为了模拟实际git rebase效果,我们先在git上提交两个修改。git log如下:

commit 7b16b280f22fe4ff57c1879867a624f6f0f14398Author: pan Date:   Sun Apr 22 08:55:32 2018 +0800    update3
commit a7186d862b95efc5cc1d7c98277af4c72bac335dAuthor: pan Date:   Sun Apr 22 08:55:16 2018 +0800    update2 
commit 16a9a4749f8ee25ab617c46925f57c2fa8a4937eAuthor: pan Date:   Sun Apr 22 08:54:55 2018 +0800    update1

假设合并这3个提交,可以按照下面过程

git rebase -i HEAD~3

执行命令后终端会出输出:

pick 16a9a47 update3 
pick a7186d8 update2
pick 7b16b28 update1
 # Rebase a9269a3..7b16b28 onto a9269a3 (3 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~                                                   

注:

第一列是rebase具体执行的操作,其中操作可以选择,其中含义如下:

选择pick操作,git会应用这个补丁,以同样的提交信息(commit message)保存提交

选择reword操作,git会应用这个补丁,但需要重新编辑提交信息

选择edit操作,git会应用这个补丁,但会因为amending而终止

选择squash操作,git会应用这个补丁,但会与之前的提交合并

选择fixup操作,git会应用这个补丁,但会丢掉提交日志

选择exec操作,git会在shell中运行这个命令


对比之前的两个提交提交,我觉得第一个提交可以保留,第二个和第三个合并到第一个就可以了。

将第二个和第三个pick改成squash或者s,然后保存退出。如下:

pick 16a9a47 update3 
s a7186d8 update2
s 7b16b28 update1

此时git会自动将第二个提交合并到第一个提交,并弹出合并提示信息,如下:

# This is a combination of 3 commits.
# This is the 1st commit message:
update3 
 # This is the commit message #2:
 update2 
 # This is the commit message #3:
 update1
 # Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Apr 22 08:54:55 2018 +0800
#
# interactive rebase in progress; onto a9269a3
# Last commands done (3 commands done):
#    s a7186d8 update
#    s 7b16b28 update6666
#  No commands remaining.

如果需要修改下提交信息,如果不需要直接保存退出即可。

# This is a combination of 3 commits. 

合并提交测试

# Please enter the commit message for your changes. Lines starting 

# with '#' will be ignored, and an empty message aborts the commit. 

# Date:      Sun Apr 22 08:54:55 2018 +0800 

# interactive rebase in progress; onto a9269a3 

# Last commands done (3 commands done): 

#    s a7186d8 update 

#    s 7b16b28 update6666 

#  No commands remaining.

此时我们已经完成了将两个提交合并为一个的处理,可以通过git log查看

commit 4a51759fae9bbd84904029473fe09f8a77f143ed
Author: pan 
Date:   Sun Apr 22 08:54:55 2018 +0800    合并提交测试

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值