git rebase簡介(高級篇)

原文http://gitbook.liuhui998.com/4_3.html
1、基本  
對於 git rebase ,你亦能夠選擇進行交互式的rebase。這種方法一般用於在向別處推送提交以前對它們進行重寫。交互式rebase提供了一個簡單易用的途徑讓你在和別人分享提交以前對你的提交進行分割、合併或者重排序。在把從其餘開發者處拉取的提交應用到本地時,你也能夠使用交互式rebase對它們進行清理。
若是你想在rebase的過程當中對一部分提交進行修改,你能夠在' git rebase '命令中加入' -i' '--interactive '參數去調用交互模式。
$ git rebase -i origin/master
這個命令會執行交互式rebase操做,操做對象是那些自最後一次從origin倉庫拉取或者向origin推送以後的全部提交。
若想查看一下將被rebase的提交,能夠用以下的log命令:
$ git log github/master..
一旦運行了'rebase -i'命令,你所預設的編輯器會被調用,其中含有以下的內容:

pick fc62e55 added file_size
pick 9824bf4 fixed little thing
pick 21d80a5 added number to log
pick 76b9da6 added the apply command
pick c264051 Revert "added file_size" - not implemented correctly

# Rebase f408319..b04dc3d onto f408319
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
這些信息表示從你上一次推送操做起有5個提交。每一個提交都用一行來表示,行格式以下:
(action) (partial-sha) (short commit message)
如今你能夠將操做(action)改成' edit '(使用提交,可是暫停以便進行修正)或者' squash '(使用提交,可是把它與前一提交合並),默認是' pick '(使用提交)。你能夠對這些行上下移動從而對提交進行重排序。當你退出編輯器時,git會按照你指定的順序去應用提交,而且作出相應的操做(action)。
2、pick操做
若是指定進行' pick '操做,git會應用這個補丁,以一樣的提交信息(commit message)保存提交。
3、squash操做
若是指定進行' squash '操做,git會把這個提交和前一個提交合併成爲一個新的提交。這會再次調用編輯器,你在裏面合併這兩個提交的提交信息。因此,若是你(在上一步)以以下的內容離開編輯器:
pick   fc62e55 added file_size
squash  9824bf4 fixed little thing
squash  21d80a5 added number to log
squash  76b9da6 added the apply command
squash  c264051 Revert "added file_size" - not implemented correctly
你必須基於如下的提交信息建立一個新的提交信息:
# This is a combination of 5 commits.
# The first commit's message is:
added file_size
# This is the 2nd commit message:
fixed little thing
# This is the 3rd commit message:
added number to log
# This is the 4th commit message:
added the apply command
# This is the 5th commit message:
Revert "added file_size" - not implemented correctly
This reverts commit fc62e5543b195f18391886b9f663d5a7eca38e84.
一旦你完成對提交信息的編輯而且退出編輯器,這個新的提交及提交信息會被保存起來。
4、edit操做
若是指定進行' edit '操做, git 會完成一樣的工做,可是在對下一提交進行操做以前,它會返回到命令行讓你對提交進行修正,或者對提交內容進行修改。
例如你想要分割一個提交,你須要對那個提交指定'edit'操做:
pick   fc62e55 added file_size
pick   9824bf4 fixed little thing
edit    21d80a5 added number to log
pick   76b9da6 added the apply command
pick   c264051 Revert "added file_size" - not implemented correctly
你會進入到命令行,重置(reset)該提交,而後建立兩個(或者更多個)新提交。假設提交21d80a5修改了兩個文件,file1和file2,你想把這兩個修改放到不一樣的提交裏。你能夠在進入命令行以後進行以下的操做:
$ git reset HEAD^
$ git add file1
$ git commit 'first part of split commit'
$ git add file2
$ git commit 'second part of split commit'
$ git rebase --continue
如今你有6個提交了,而不是5個。
5、丟棄提交操做
交互式 rebase 的最後一個做用是丟棄提交。若是把一行刪除而不是指定' pick '、' squash '和‘ edit ' '中的任何一個,git會從歷史中移除該提交。
相關文章
相關標籤/搜索