初始場(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 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歷史記錄,可追溯性較差。
如下圖:
另外,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操作。
參考: