兩種合併分支的方式:merge
,rebase
With the
rebase
command, you can take all the changes that were committed on one branch and replay them on a different branch
rebase
是如何合併分支的
如下圖:採用rebase
方式將experiment
分支上的內容合併到master
分支上
git rebase master
這個命令幕後都做了什麼操作:
- 先
experiment
分支的修改暫存起來 - 將
experiment
分支reset到master
分支的狀態 - 在重置後的
experiment
分支上replay剛剛暫存起來的修改
最後:切換到master
分支,進行簡單的fast-forward
合併即可
爲什麼要用rebase
(和merge
的區別)
如上例中所示,rebase
的提交歷史更乾淨線性
何時(不)使用rebase
In general the way to get the best of both worlds is to
rebase
local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but neverrebase
anything you’ve pushed somewhere.
-
在本地開發時可以使用,讓我們的提交歷史更乾淨易讀
-
不要使用
rebase
合併已經push到遠程倉庫的內容
更多討論可訪問github:
https://github.com/pluscai/use-git/issues/18