git merge和git rebase區(qū)別筆記

初始場(chǎng)景:
基于正常的開發(fā)分支修改幾個(gè)小bug,然后在合并到開發(fā)分支上。


初始分支

git merge

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

合并后的節(jié)點(diǎn)會(huì)按照commit時(shí)間順序排列。
git merge操作會(huì)在當(dāng)前分支上生成一個(gè)新的commit節(jié)點(diǎn),并保留所有的操作歷史節(jié)點(diǎn),對(duì)問題的追溯很有益處,但是會(huì)產(chǎn)生大量無(wú)用commit節(jié)點(diǎn),使提交歷史記錄很冗長(zhǎng)。
如下圖:

git merge

git rebase

git checkout feature
git rebase hotfix

rebase操作后的歷史并不會(huì)按commit時(shí)間順序排列。
rebase操作會(huì)找出當(dāng)前分支(feature)的所有修改點(diǎn),并將其生產(chǎn)一系列布?。?-6-8);然后以被rebase分支(hotfix)最后一個(gè)節(jié)點(diǎn)(9)為開始點(diǎn),將feature上生成的布丁應(yīng)用到hotfix上(1-2-3-5-7-9-4-6-8)。
如上描述,rebase操作能使提交歷史更干凈美觀,可忽略很多不必要的節(jié)點(diǎn);但是這樣等同于重寫commit歷史記錄,可追溯性較差。
如下圖:

git rebase

另外,rebase提供了一些補(bǔ)充操作

git checkout feature
git rebase -i hotfix

執(zhí)行以上命令會(huì)彈出編輯框,如下,(IDE可能會(huì)有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

解釋下幾個(gè)參數(shù):

  • pick就是cherry-pick
  • reword 就是在cherry-pick的同時(shí)你可以編輯
  • commit message,它會(huì)在執(zhí)行的時(shí)候跳出一個(gè)界面讓你編輯信息,當(dāng)你退出的時(shí)候,會(huì)繼續(xù)執(zhí)行命令
  • edit 麻煩點(diǎn),cherry-pick同時(shí) ,會(huì)停止,讓你編輯信息,完了后,你要用git rebase --continue命令繼續(xù)執(zhí)行,相對(duì)上面來(lái)說有點(diǎn)麻煩。
  • squash,合并此條記錄到前一個(gè)記錄中,并把commit message也合并進(jìn)去 。
  • fixup ,合并此條記錄到前一個(gè)記錄中,但是忽略此條commit message

我們可以使用-i參數(shù)達(dá)到重新排序commit歷史、編輯提交信息、刪除無(wú)用提交、合并同問題提交等操作。

rebase使用原則:
一旦分支中的提交對(duì)象發(fā)布到公共倉(cāng)庫(kù),就不要對(duì)該分支進(jìn)行rebase操作。

參考:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容