git merge和git rebase区别笔记

2018-02-24  本文已影响322人  Rflyee

初始场景:
基于正常的开发分支修改几个小bug,然后在合并到开发分支上。


初始分支

git merge

git checkout feature
git merge hotfix
或者
git merge hotfix feature

合并后的节点会按照commit时间顺序排列。
git merge操作会在当前分支上生成一个新的commit节点,并保留所有的操作历史节点,对问题的追溯很有益处,但是会产生大量无用commit节点,使提交历史记录很冗长。
如下图:

git merge

git rebase

git checkout feature
git rebase hotfix

rebase操作后的历史并不会按commit时间顺序排列。
rebase操作会找出当前分支(feature)的所有修改点,并将其生产一系列布丁(4-6-8);然后以被rebase分支(hotfix)最后一个节点(9)为开始点,将feature上生成的布丁应用到hotfix上(1-2-3-5-7-9-4-6-8)。
如上描述,rebase操作能使提交历史更干净美观,可忽略很多不必要的节点;但是这样等同于重写commit历史记录,可追溯性较差。
如下图:

git rebase

另外,rebase提供了一些补充操作

git checkout feature
git rebase -i hotfix

执行以上命令会弹出编辑框,如下,(IDE可能会有GUI交互窗)。

  14 pick 9fa8fe5c4 add 1
  15 pick 94e3c2cb7 add 2
  16 pick 1bc1c9152 add 3
  17 
  18 # Rebase 6041e1b91..5b88fbfaa onto 6041e1b91 (16 commands)
  19 #
  20 # Commands:
  21 # p, pick = use commit
  22 # r, reword = use commit, but edit the commit message
  23 # e, edit = use commit, but stop for amending
  24 # s, squash = use commit, but meld into previous commit
  25 # f, fixup = like "squash", but discard this commit's log message
  26 # x, exec = run command (the rest of the line) using shell
  27 # d, drop = remove commit
  28 #
  29 # These lines can be re-ordered; they are executed from top to bottom.
  30 #
  31 # If you remove a line here THAT COMMIT WILL BE LOST.
  32 #
  33 # However, if you remove everything, the rebase will be aborted.
  34 #
  35 # Note that empty commits are commented out

解释下几个参数:

我们可以使用-i参数达到重新排序commit历史、编辑提交信息、删除无用提交、合并同问题提交等操作。

rebase使用原则:
一旦分支中的提交对象发布到公共仓库,就不要对该分支进行rebase操作。

参考:

上一篇 下一篇

猜你喜欢

热点阅读