Name
git-rebase - Reapply commits on top of another base tip
SYNOPSIS
reabse
翻成中文大概是「重新定義分支的參考基準」。
假設我們現在在topic
branch上:
輸入下列指令後:
歷史紀錄會變成這樣:
看起來像是把 A、B、C 從 E 上面,剪下貼上到 G 上面,
不過實際上是這樣:
A'、B'、C'是新創建的 commit,SHA-1與舊的 A、B、C commit都會不一樣。
而舊的 A、B、C commit 仍然存在,並未被刪除,
不過因為已經沒有 branch 指向它,所以沒有顯示出來,
就在那邊等待 Git 的資源回收機制處理他們。
不過因為rebase
等於是修改歷史,
所以不應該將已經push
出去的內容任意進行rebase
,
不然可能會造成團隊協作的困擾。
我個人比較常用的應用是修改還未push
上去的 commit message,
有時候可能已經 commit 很多了,
但發現中間的 commit message 需要修改:
這時候只要使用rebase
:
-i
是--interactive
的縮寫,指將 commit 列出,讓使用者可以在rebase
前進行修改。
-i
--interactive
Make a list of the commits which are about to be rebased. Let the user edit that list before rebasing.
會出現:
這時候我們按照提示,使用 vim 指令i
(指INSERTr),將 "pick" 改成 "r"或"reword":
再使用vim指令ESC :wq Enter
,就可以修改 commit message 了。
https://git-scm.com/docs/git-rebase
https://medium.com/程式乾貨/git-rebase-使用場景-604d753e53f6
https://gitbook.tw/chapters/branch/merge-with-rebase