这一次彻底搞懂 Git Rebase
使用 Git 已經(jīng)好幾年了,卻始終只是熟悉一些常用的操作。對(duì)于 Git Rebase 卻很少用到,直到這一次,不得不用。
一、起因
上線構(gòu)建的過(guò)程中掃了一眼代碼變更,突然發(fā)現(xiàn),?commit?提交竟然多達(dá)?62?次。我們來(lái)看看都提交了什么東西:
這里我們先不說(shuō)?git?提交規(guī)范,就單純這么多次無(wú)用的?commit?就很讓人不舒服。可能很多人覺(jué)得無(wú)所謂,無(wú)非是多了一些提交紀(jì)錄。
然而,并非如此,你可能聽(tīng)過(guò)破窗效應(yīng),編程也是如此!
二、導(dǎo)致問(wèn)題
1.不利于代碼?review
設(shè)想一下,你要做?code review?,結(jié)果一個(gè)很小的功能,提交了?60?多次,會(huì)不會(huì)有一些崩潰?
2.會(huì)造成分支污染
你的項(xiàng)目充滿了無(wú)用的?commit?紀(jì)錄,如果有一天線上出現(xiàn)了緊急問(wèn)題,你需要回滾代碼,卻發(fā)現(xiàn)海量的?commit?需要一條條來(lái)看。
遵循項(xiàng)目規(guī)范才能提高團(tuán)隊(duì)協(xié)作效率,而不是隨心所欲。
三、如何合并多次提交紀(jì)錄?
基于上面所說(shuō)問(wèn)題,我們不難想到:每一次功能開(kāi)發(fā), 對(duì)多個(gè) commit 進(jìn)行合并處理。
這時(shí)候就需要用到?git rebase?了。這個(gè)命令沒(méi)有太難,不常用可能源于不熟悉,所以我們來(lái)通過(guò)示例學(xué)習(xí)一下。
1.我們來(lái)合并最近的 4 次提交紀(jì)錄,執(zhí)行:
git rebase -i HEAD~42.這時(shí)候,會(huì)自動(dòng)進(jìn)入?vi?編輯模式:
s cacc52da add: qrcode s f072ef48 update: indexeddb hack s 4e84901a feat: add indexedDB floder s 8f33126c feat: add test2.js# Rebase 5f2452b2..8f33126c onto 5f2452b2 (4 commands) # # 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 # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. #有幾個(gè)命令需要注意一下:
- 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
- d, drop = remove commit
按照如上命令來(lái)修改你的提交紀(jì)錄:
s cacc52da add: qrcode s f072ef48 update: indexeddb hack s 4e84901a feat: add indexedDB floder p 8f33126c feat: add test2.js3.如果保存的時(shí)候,你碰到了這個(gè)錯(cuò)誤:
error: cannot 'squash' without a previous commit注意不要合并先前提交的東西,也就是已經(jīng)提交遠(yuǎn)程分支的紀(jì)錄。
4.如果你異常退出了?vi?窗口,不要緊張:
git rebase --edit-todo這時(shí)候會(huì)一直處在這個(gè)編輯的模式里,我們可以回去繼續(xù)編輯,修改完保存一下:
git rebase --continue5.查看結(jié)果
git log三次提交合并成了一次,減少了無(wú)用的提交信息。
四、Rebase 的另外一種使用場(chǎng)景:分支合并
1.我們先從?master?分支切出一個(gè)?dev?分支,進(jìn)行開(kāi)發(fā):
git:(master) git checkout -b feature12.這時(shí)候,你的同事完成了一次?hotfix?,并合并入了?master?分支,此時(shí)?master?已經(jīng)領(lǐng)先于你的?feature1?分支了:
3.恰巧,我們想要同步?master?分支的改動(dòng),首先想到了?merge?,執(zhí)行:
git:(feature1) git merge master?圖中綠色的點(diǎn)就是我們合并之后的結(jié)果,執(zhí)行:
git:(feature1) git log就會(huì)在記錄里發(fā)現(xiàn)一些?merge?的信息,但是我們覺(jué)得這樣污染了?commit?記錄,想要保持一份干凈的?commit?,怎么辦呢?這時(shí)候,?git rebase?就派上用場(chǎng)了。
4.讓我們來(lái)試試?git rebase?,先回退到同事?hotfix?后合并?master?的步驟:
5.使用?rebase?后來(lái)看看結(jié)果:
git:(feature1) git rebase master這里補(bǔ)充一點(diǎn):?rebase?做了什么操作呢?
首先,?git?會(huì)把?feature1?分支里面的每個(gè)?commit?取消掉;
其次,把上面的操作臨時(shí)保存成?patch?文件,存在?.git/rebase?目錄下;
然后,把?feature1?分支更新到最新的?master?分支;
最后,把上面保存的?patch?文件應(yīng)用到?feature1?分支上;
從?commit?記錄我們可以看出來(lái),?feature1?分支是基于?hotfix?合并后的?master?,自然而然的成為了最領(lǐng)先的分支,而且沒(méi)有?merge?的?commit?記錄,是不是感覺(jué)很舒服了。
6.在?rebase?的過(guò)程中,也許會(huì)出現(xiàn)沖突?conflict?。在這種情況,?git?會(huì)停止?rebase?并會(huì)讓你去解決沖突。在解決完沖突后,用?git add?命令去更新這些內(nèi)容。
注意,你無(wú)需執(zhí)行 git-commit,只要執(zhí)行 continue
git rebase --continue這樣?git?會(huì)繼續(xù)應(yīng)用余下的?patch?補(bǔ)丁文件。
7.在任何時(shí)候,我們都可以用?--abort?參數(shù)來(lái)終止?rebase?的行動(dòng),并且分支會(huì)回到?rebase?開(kāi)始前的狀態(tài)。
git rebase —abort?
總結(jié)
以上是生活随笔為你收集整理的这一次彻底搞懂 Git Rebase的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 很遗憾,没有一篇文章能讲清楚ZooKee
- 下一篇: 史上最易懂的 Kubernetes 儿童