git操作之git rebase

git rebase可以简单的理解为调整commit信息,包括合并commit信息到master(与merge类似,但是效果不同)以及删除commit信息或者修改log信息等,下面分别介绍几种git rebase常用的场景。

合并commit信息到master

例如有如下commit信息并且当前处于topic分支:

      A---B---C topic
     /
D---E---F---G master

当我们执行:git rebase master 命令后,commit信息将会变为:

              A'--B'--C' topic
             /
D---E---F---G master

如果我们执行:git rebase topic master,则commit信息会变为:

      A'--B'--C' topic
     / 
D---E---A'---B'---C'---F---G master

上面两种情况相当于把commit信息合并到一起,而且是线性合并,这就是与merge不同的地方,如果使用git merge进行合并,则结果为:

      A---B---C      topic
     /          \
D---E---F---G ---H  master

多数情况下,当执行git rebase时,会出现conflict,此时,我们需要手动处理冲突,然后执行git add 把修改后的文件添加到暂存区,最后,如果想完成rebase,则执行git rebase –continue,如果不想,则执行git rebase –abort

使用交互模式编辑commit信息

使用git rebase -i 交互模式,你可以编辑commit信息,包括改变commit的顺序、删除某些commit、修改log信息等。使用方法为:git rebase -i ,例如:
执行git rebase -i HEAD~4,编辑器将会自动打开,显示最新的4个commit信息,以及一些操作命令,效果如下:

pick 9a54fd4 添加commit的说明
pick 0d4a808 添加pull的说明
pick d286baa The oneline of this commit
pick f1a5c00 The oneline of the next commit

# Rebase 326fc9f..0d4a808 onto d286baa
#
# 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
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

这些命令都很容易理解,比如你想修改0d4a808 commit的log信息,则把第二行的pick改为reword即可,然后保存退出,此时会再次打开一个编辑器,让你修改0d4a808 commit的log信息,修改后保存即可。

如果你想在0d4a808 和 d286baa 两个commit之间补上一个新的commit,可以使用edit替换第二行的pick然后保存,按照提示用git commit –amend添加一个新的commit,然后执行git rebase –continue即可。

如果你想删除9a54fd4和d286baa,则直接删了9a54fd4,并且把d286baa前面的命令改为fixup,意思就是删除本条commit。注意,不能直接把d286baa的记录删除,因为只有在前面没有commit的情况下才可以直接删除,效果如下:

(这里删除了   pick 9a54fd4 添加commit的说明)
pick 0d4a808 添加pull的说明
fixup d286baa The oneline of this commit
pick f1a5c00 The oneline of the next commit
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值