git rebase(高級)

jopen 10年前發布 | 29K 次閱讀 Git 版本控制系統

原文http://gitbook.liuhui998.com/4_3.html

一、基本  

對于 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)。

二、pick操作

如果指定進行' pick '操作,git會應用這個補丁,以同樣的提交信息(commit message)保存提交。

三、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.

一旦你完成對提交信息的編輯并且退出編輯器,這個新的提交及提交信息會被保存起來。

四、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個。

五、丟棄提交操作

交互式 rebase 的最后一個作用是丟棄提交。如果把一行刪除而不是指定' pick '、' squash '和‘ edit ' '中的任何一個,git會從歷史中移除該提交。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!