当前位置:   article > 正文

git操作之git rebase_git rebase后commit会变吗

git rebase后commit会变吗

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

合并commit信息到master

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

      A---B---C topic
     /
D---E---F---G master
  • 1
  • 2
  • 3

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

              A'--B'--C' topic
             /
D---E---F---G master
  • 1
  • 2
  • 3

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

      A'--B'--C' topic
     / 
D---E---A'---B'---C'---F---G master
  • 1
  • 2
  • 3

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

      A---B---C      topic
     /          \
D---E---F---G ---H  master
  • 1
  • 2
  • 3

多数情况下,当执行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.
#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

这些命令都很容易理解,比如你想修改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
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/789278
推荐阅读
相关标签
  

闽ICP备14008679号