一圖弄懂Git rebase

兩種合併分支的方式: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這個命令幕後都做了什麼操作:

  1. experiment分支的修改暫存起來
  2. experiment分支reset到master分支的狀態
  3. 在重置後的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 never rebase anything you’ve pushed somewhere.

  • 在本地開發時可以使用,讓我們的提交歷史更乾淨易讀

  • 不要使用rebase合併已經push到遠程倉庫的內容

更多討論可訪問github:
https://github.com/pluscai/use-git/issues/18

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章