git rebase簡介(基本篇)
原文:






一、基本
git rebase用於把一個分支的修改合併到當前分支。
假設你現在基於遠端分支"origin",建立一個叫"mywork"的分支。
$ git checkout -b mywork origin
假設遠端分支"origin"已經有了2個提交,如圖

現在我們在這個分支做一些修改,然後生成兩個提交(commit).
$ vi file.txt
$ git commit
$ vi otherfile.txt
$ git commit
...
但是與此同時,有些人也在"origin"分支上做了一些修改並且做了提交了. 這就意味著"origin"和"mywork"這兩個分支各自"前進"了,它們之間"分叉"了。

在這裡,你可以用"pull"命令把"origin"分支上的修改拉下來並且和你的修改合併; 結果看起來就像一個新的"合併的提交"(merge commit):

但是,如果你想讓"mywork"分支歷史看起來像沒有經過任何合併一樣,你也許可以用 git rebase:
$ git checkout mywork
$ git rebase origin
這些命令會把你的"mywork"分支裡的每個提交(commit)取消掉,並且把它們臨時 儲存為補丁(patch)(這些補丁放到".git/rebase"目錄中),然後把"mywork"分支更新 為最新的"origin"分支,最後把儲存的這些補丁應用到"mywork"分支上。

當'mywork'分支更新之後,它會指向這些新建立的提交(commit),而那些老的提交會被丟棄。 如果執行垃圾收集命令(pruning garbage collection), 這些被丟棄的提交就會刪除. (請檢視 git gc)

二、解決衝突
在rebase的過程中,也許會出現衝突(conflict).
在這種情況,Git會停止rebase並會讓你去解決 衝突;在解決完衝突後,用"git-add"命令去更新這些內容的索引(index), 然後,你無需執行 git-commit,只要執行:
$ git rebase --continue
這樣git會繼續應用(apply)餘下的補丁。
在任何時候,你可以用--abort引數來終止rebase的行動,並且"mywork" 分支會回到rebase開始前的狀態。
$ git rebase --abort
三、git rebase和git merge的區別
現在我們可以看一下用合併(merge)和用rebase所產生的歷史的區別:

當我們使用Git log來參看commit時,其commit的順序也有所不同。
假設C3提交於9:00AM,C5提交於10:00AM,C4提交於11:00AM,C6提交於12:00AM,
對於使用git merge來合併所看到的commit的順序(從新到舊)是:C7 ,C6,C4,C5,C3,C2,C1
對於使用git rebase來合併所看到的commit的順序(從新到舊)是:C7 ,C6‘,C5',C4,C3,C2,C1
因為C6'提交只是C6提交的克隆,C5'提交只是C5提交的克隆,
從使用者的角度看使用git rebase來合併後所看到的commit的順序(從新到舊)是:C7 ,C6,C5,C4,C3,C2,C1
另外,我們在使用git pull命令的時候,可以使用--rebase引數,即git pull --rebase,這裡表示把你的本地當前分支裡的每個提交(commit)取消掉,並且把它們臨時
儲存為補丁(patch)(這些補丁放到".git/rebase"目錄中),然後把本地當前分支更新
為最新的"origin"分支,最後把儲存的這些補丁應用到本地當前分支上。關於git
pull的更多內容請參考《git
pull簡介》相關文章
- Git基礎命令之git rebase命令Git
- [Git]rebaseGit
- git rebaseGit
- Git -- RebaseGit
- git rebase(變基)—— Git 學習筆記 19Git筆記
- git:rebase(變基)的使用和理解Git
- 對git的rebase(變基)的理解Git
- git rebase 和 git mergeGit
- Git——rebase命令Git
- git rebase -iGit
- git rebase 流程Git
- git rebase masterGitAST
- git學習之git rebaseGit
- 如何理解git rebase?Git
- git rebase的使用Git
- [Git] Git整理(四) git rebase 的使用Git
- 基於Git rebase修改歷史提交資訊Git
- Git rebase 與 Git merge 的使用Git
- Git 由淺入深之細說變基 (rebase)Git
- Git 使用簡介Git
- Git命令簡介Git
- Git 配置簡介Git
- Git(12)-- Git 分支 - 分支簡介Git
- 學會使用 git-rebaseGit
- git-rebase進階使用Git
- 撤銷rebase與git原理Git
- 徹底搞懂 Git-RebaseGit
- 詳解git rebase,讓你走上git大神之路Git
- git rebase --onto 的奇妙用法Git
- Git教程十九:分支管理之RebaseGit
- git merge和rebase的區別Git
- Git_簡單介紹Git
- 你根本不懂rebase-使用rebase打造可讀的git graphGit
- git rebase後出現(master|REBASE 1/10)的解決辦法GitAST
- Git中merge和rebase的區別Git
- git rebase沖掉記錄找回方法Git
- git分支管理--rebase&merge詳解Git
- git在工作中正確的使用方式----git rebase篇Git
- 面試官:主說 Git Rebase 和 Git Merge 差異面試Git