git rebase
rebase簡介
rebase, 意思為變基,即改變分支的的根支。提到rebase就不得不說說merge,他們兩個都可以完成相同的的工作(結果),將兩個分支進行合并,但他們工作方式完全不同。
merge和rebase最大的不同,merge會保留所有的提交歷史記錄,而rebase會刪除基變分支的歷史記錄,如下圖所示。
merge工作方式圖:

merge.png
rebase工作方式圖:

rebase.png
rebase的基本操作
#
# C5---C6---C7 dev1
# /
# C1---C2---C3---C4 dev
git rebase dev
# A'--B'--C' dev1
# /
# D---E---F---G dev
# 在 rebase 的過程中,也許會出現(xiàn)沖突 conflict 。在這種情況, git 會停止 rebase 并會讓你去解決沖突。在解決完沖突后,用 git add 命令去更新這些內容。
# 注意,你無需執(zhí)行 git-commit,只要執(zhí)行 continue
git rebase --continue
# 這樣 git 會繼續(xù)應用余下的 patch 補丁文件。
# 在任何時候,我們都可以用 --abort 參數(shù)來終止 rebase 的行動,并且分支會回到 rebase 開始前的狀態(tài)。
git rebase —abort
rebase的另一種操作:合并分支
命令:
git rebase -i HEAD~3
上面命令的意思是合并最近的 4 次提交紀錄。
# 進入編輯模式
p 9abd07c add bbb
s 16981e9 add ccc
s e0f8d72 add d
# Rebase 7e215b2..e0f8d72 onto 7e215b2 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
pick:保留該commit(縮寫:p)
reword:保留該commit,但我需要修改該commit的注釋(縮寫:r)
edit:保留該commit, 但我要停下來修改該提交(不僅僅修改注釋)(縮寫:e)
squash:將該commit和前一個commit合并(縮寫:s)
fixup:將該commit和前一個commit合并,但我不要保留該提交的注釋信息(縮寫:f)
exec:執(zhí)行shell命令(縮寫:x)
drop:我要丟棄該commit(縮寫:d)
如果你異常退出了 vim 窗口,可以執(zhí)行
git rebase --edit-todo
這時候會一直處在這個編輯的模式里,我們可以回去繼續(xù)編輯,修改完保存一下:
git rebase --continue
rebase最佳實踐
- 不要對master分支進行rebase
- 一般來說,執(zhí)行rebase的分支都是自己本地的分支,沒有推送到遠程版本庫