徹底搞懂Git Rebase


使用 Git 已經好幾年了,卻始終只是熟悉一些常用的操作。對於 Git Rebase 卻很少用到,直到這一次,不得不用。

一、起因

上線構建的過程中掃了一眼代碼變更,突然發現,commit 提交竟然多達 62 次。我們來看看都提交了什么東西:
commit1

這里我們先不說 git 提交規范,就單純這么多次無用的 commit 就很讓人不舒服。可能很多人覺得無所謂,無非是多了一些提交紀錄。

然而,並非如此,你可能聽過破窗效應,編程也是如此!

二、導致問題

1.不利於代碼 review
設想一下,你要做 code review ,結果一個很小的功能,提交了 60 多次,會不會有一些崩潰?

2.會造成分支污染
你的項目充滿了無用的 commit 紀錄,如果有一天線上出現了緊急問題,你需要回滾代碼,卻發現海量的 commit 需要一條條來看。

遵循項目規范才能提高團隊協作效率,而不是隨心所欲。

三、Rebase 場景一:如何合並多次提交紀錄?

基於上面所說問題,我們不難想到:每一次功能開發, 對多個 commit 進行合並處理。

這時候就需要用到 git rebase 了。這個命令沒有太難,不常用可能源於不熟悉,所以我們來通過示例學習一下。

1.我們來合並最近的 4 次提交紀錄,執行:

git rebase -i HEAD~4

2.這時候,會自動進入 vi 編輯模式:

s cacc52da add: qrcodes f072ef48 update: indexeddb hacks 4e84901a feat: add indexedDB floders 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.#

有幾個命令需要注意一下:

  • 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

按照如上命令來修改你的提交紀錄:

s cacc52da add: qrcodes f072ef48 update: indexeddb hacks 4e84901a feat: add indexedDB floderp 8f33126c feat: add test2.js

3.如果保存的時候,你碰到了這個錯誤:

error: cannot 'squash' without a previous commit

注意不要合並先前提交的東西,也就是已經提交遠程分支的紀錄。

4.如果你異常退出了 vi 窗口,不要緊張:

git rebase --edit-todo

這時候會一直處在這個編輯的模式里,我們可以回去繼續編輯,修改完保存一下:

git rebase --continue

5.查看結果

git log

三次提交合並成了一次,減少了無用的提交信息。

commit2

四、Rebase 場景二:分支合並

1.我們先從 master 分支切出一個 dev 分支,進行開發:

git:(master) git checkout -b feature1

git1
2.這時候,你的同事完成了一次 hotfix,並合並入了 master 分支,此時 master 已經領先於你的 feature1 分支了:
git2
3.恰巧,我們想要同步 master 分支的改動,首先想到了 merge,執行:

git:(feature1) git merge master

git3
圖中綠色的點就是我們合並之后的結果,執行:

git:(feature1) git log

就會在記錄里發現一些 merge 的信息,但是我們覺得這樣污染了 commit 記錄,想要保持一份干凈的 commit,怎么辦呢?這時候,git rebase 就派上用場了。

4.讓我們來試試 git rebase ,先回退到同事 hotfix 后合並 master 的步驟:
git4
5.使用 rebase 后來看看結果:

git:(feature1) git rebase master

這里補充一點:rebase 做了什么操作呢?

首先,git 會把 feature1 分支里面的每個 commit 取消掉;
其次,把上面的操作臨時保存成 patch 文件,存在 .git/rebase 目錄下;
然后,把 feature1 分支更新到最新的 master 分支;
最后,把上面保存的 patch 文件應用到 feature1 分支上;

git5

commit 記錄我們可以看出來,feature1 分支是基於 hotfix 合並后的 master ,自然而然的成為了最領先的分支,而且沒有 mergecommit 記錄,是不是感覺很舒服了。

6.在 rebase 的過程中,也許會出現沖突 conflict。在這種情況,git 會停止 rebase 並會讓你去解決沖突。在解決完沖突后,用 git add 命令去更新這些內容。

注意,你無需執行 git-commit,只要執行 continue

git rebase --continue

這樣 git 會繼續應用余下的 patch 補丁文件。

7.在任何時候,我們都可以用 --abort 參數來終止 rebase 的行動,並且分支會回到 rebase 開始前的狀態。

git rebase —abort

五、更多 Rebase 的使用場景

git-rebase 存在的價值是:對一個分支做「變基」操作。

1.當我們在一個過時的分支上面開發的時候,執行 rebase 以此同步 master 分支最新變動;
2.假如我們要啟動一個放置了很久的並行工作,現在有時間來繼續這件事情,很顯然這個分支已經落后了。這時候需要在最新的基准上面開始工作,所以 rebase 是最合適的選擇。

六、為什么會是危險操作?

根據上文來看,git-rebase 很完美,解決了我們的兩個問題:
1.合並 commit 記錄,保持分支整潔;
2.相比 merge 來說會減少分支合並的記錄;

如果你提交了代碼到遠程,提交前是這樣的:
git2

提交后遠程分支變成了這樣:
git5

而此時你的同事也在 feature1 上開發,他的分支依然還是:
git6

那么當他 pull 遠程 master 的時候,就會有丟失提交紀錄。這就是為什么我們經常聽到有人說 git rebase 是一個危險命令,因為它改變了歷史,我們應該謹慎使用。

除非你可以肯定該 feature1 分支只有你自己使用,否則請謹慎操作。

結論:只要你的分支上需要 rebase 的所有 commits 歷史還沒有被 push 過,就可以安全地使用 git-rebase來操作。

本文轉載自:http://jartto.wang/2018/12/11/git-rebase/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM