在之前Git合并代码的几个场景提到的场景1中可以将某个分支的commit记录完全复制到另外一个分支上。其中只提到的简单的将一次修改的commit记录复制到另外一个分支上,但是我们在实际开发中很有可能是需要复制好几次的commit记录,那么如果一个一个commit记录复制显示很out,那么git rebase命令就来拯救这个out的操作。
还是举个实际的例子来演示下该操作。通过git log 查看所有的提交记录
ommit f69dd44d1e9f293d36b410cf156b1f4a83cf942e
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:43:29 2019 +0800
add b activity
commit e589ee1fe861c0a8d173aa06d0fe919d4ffbc414
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:43:04 2019 +0800
add a layout
commit ca54fef26c6c0dafaf6926b014cc7456270fbda7
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:42:38 2019 +0800
add a activity
commit f1195b95a79573c593a542d2d65d3679f40e30e6
Author: wenjing.liu <jingmiancao@xx.com>
Date: Tue May 23 11:44:15 2017 +0800
fix bug #21098
我们现在需要将add a layout合并到add a activity上面,那么git rebase -i就需要隆重出场了。
(1)执行git rebase -i commit id 命令,该commit id为需要合并的commit的上一个commit id,也就是fix bug #21098的commit id
pick ca54fef add a activity
pick e589ee1 add a layout
pick f69dd44 add b activity
# Rebase f1195b9..f69dd44 onto f1195b9 (3 command(s))
#
# 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 outick ca54fef add a activity
我们可以看到注释里面有一个命令,挑几个这次用到的
pick:执行该次提交,也就是不对这些提交发生改变
reword:执行该次提交,但是会去修改提交记录的备注内容
edit:执行该次提交,但是会去追加提交记录的备注内容
squash:执行该次提交,但是会将提交内容合并到前一个commit中
fixup:和squash相同,但是会舍弃合并分支中的commit备注内容
我们需要将add a layout合并到add a activity,所以那我们就需要修改上面的记录,将add b activity删除,将pick add a layout改成squash add a layout,即如下
pick ca54fef add a activity
squash e589ee1 add a layout
(2)按esc退出编辑状态,保存之后, 又跳到一个commit编辑界面:
# This is a combination of 2 commits.
# The first commit's message is:
add a activity
# This is the 2nd commit message:
add a layout
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Fri Feb 15 10:42:38 2019 +0800
#
# interactive rebase in progress; onto f1195b9
# Last commands done (2 commands done):
# pick ca54fef add a activity
# squash e589ee1 add layout
# No commands remaining.
# You are currently editing a commit while rebasing branch '20170519_jingmiancao_3.8.32' on 'f1195b9'.
#
# Changes to be committed:
# modified: app/src/main/AndroidManifest.xml
#
(3)确认无误之后,同样保存退出编辑界面,就显示如下:
$ git rebase -i f1195b95a79573c593a542d2d65d3679f40e30e6
[detached HEAD 6572e83] pppick ca54fef add a activity pppick ca54fef add a activity pppick ca54fef add a activity pick e589ee1 add layout pick f69dd44 add b activity
Date: Fri Feb 15 10:42:38 2019 +0800
1 file changed, 2 deletions(-)
Successfully rebased and updated refs/heads/20170519_jingmiancao_3.8.32.
说明已经合并成功,git pull 同步代码,如果有冲突解决冲突之后在提交即可。最后执行git push提交到服务器。
(4)通过git log查看,已经将两次commit记录显示成了一条。
commit 6572e83c05725a1303de7f6a5756a00b8eea4532
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:42:38 2019 +0800
add a activity
add a layout
commit f69dd44d1e9f293d36b410cf156b1f4a83cf942e
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:43:29 2019 +0800
add b activity
commit e589ee1fe861c0a8d173aa06d0fe919d4ffbc414
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:43:04 2019 +0800
add a layout
commit ca54fef26c6c0dafaf6926b014cc7456270fbda7
Author: wenjing.liu <jingmiancao@xx.com>
Date: Fri Feb 15 10:42:38 2019 +0800
add a activity
可以通过gitk查看这次修改的内容,发现之前add a layout和add a activity修改的内容也都在这次commit记录中了。