初始場景:
基于正常的開發(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)生大量無用commit節(jié)點(diǎn)揍拆,使提交歷史記錄很冗長。
如下圖:
git rebase
git checkout feature
git rebase hotfix
rebase操作后的歷史并不會(huì)按commit時(shí)間順序排列茶凳。
rebase操作會(huì)找出當(dāng)前分支(feature)的所有修改點(diǎn)嫂拴,并將其生產(chǎn)一系列布丁(4-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歷史記錄饱普,可追溯性較差运挫。
如下圖:
另外,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ì)上面來說有點(diǎn)麻煩检激。
- squash肴捉,合并此條記錄到前一個(gè)記錄中,并把commit message也合并進(jìn)去 叔收。
- fixup 齿穗,合并此條記錄到前一個(gè)記錄中,但是忽略此條commit message
我們可以使用-i
參數(shù)達(dá)到重新排序commit歷史饺律、編輯提交信息窃页、刪除無用提交、合并同問題提交等操作蓝晒。
rebase使用原則:
一旦分支中的提交對(duì)象發(fā)布到公共倉庫腮出,就不要對(duì)該分支進(jìn)行rebase操作。
參考: