1.rebase(變基)操作
注意事項:rebase 改變分支的根源,絕對不要在與其他人共享的分支上進行操作rebase黃金法則:絕不要在公共的分支上使用它!
1.1git merge 與 git rebase的區別
1.1.1git merge 合並兩個分支並生成一個新的提交
1.1.2git rebase提取操作有點像git cherry-pick一樣,執行rebase后依次將當前(執行rebase時所在分支)的提交cherry-pick到目標分支(待rebase的分支)上,然后將在原始分支(執行rebase時所在分支)上的已提交的commit刪除。
1.1.3 merge結果能夠體現出時間線,但是rebase會打亂時間線
在項目中經常使用git pull來拉取代碼,git pull相當於是git fetch + git merge;
在項目中運行git pull -r,也就是git pull --rebase,相當於git fetch + git rebase;
1.2當前分支master
1.3基於當前的master分支創建一個rebase_dev
1.4基於rebase_dev分支增加兩次提交
1.5切回master分支,增加兩次新的提交
1.6切回rebase_dev,查看當前git log
1.7在rebase_dev分支上進行變基操作
$git rebase master
可以看到rebase的操作,打亂了時間線;版本樹形成一條
1.8回顧一下merge的操作
1.8.1初始化一個倉庫,基於master,增加兩次提交
1.8.2基於master分支,創建merge_dev分支,增加兩次提交
1.8.3基於merge_dev,切換至master分支
1.8.4基於master分支增加一次提交,而后切換至merge_dev
1.8.5將master分支合並至merge_dev並完成提交
2.git rebase -i 命令操作
usage: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase> | --keep-base] [<upstream> [<branch>]]
or: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
or: git rebase --continue | --abort | --skip | --edit-todo
--onto <revision> rebase onto given branch instead of upstream
--keep-base use the merge-base of upstream and branch as the current base
--no-verify allow pre-rebase hook to run
-q, --quiet be quiet. implies --no-stat
-v, --verbose display a diffstat of what changed upstream
-n, --no-stat do not show diffstat of what changed upstream
--signoff add a Signed-off-by trailer to each commit
--committer-date-is-author-date
make committer date match author date
--reset-author-date ignore author date and use current date
-C <n> passed to 'git apply'
--ignore-whitespace ignore changes in whitespace
--whitespace <action>
passed to 'git apply'
-f, --force-rebase cherry-pick all commits, even if unchanged
--no-ff cherry-pick all commits, even if unchanged
**--continue continue**
--skip skip current patch and continue
--abort abort and check out the original branch
--quit abort but keep HEAD where it is
** --edit-todo edit the todo list during an interactive rebase**
--show-current-patch show the patch file being applied or merged
--apply use apply strategies to rebase
-m, --merge use merging strategies to rebase
-i, --interactive let the user edit the list of commits to rebase
--rerere-autoupdate update the index with reused conflict resolution if possible
--empty <{drop,keep,ask}>
how to handle commits that become empty
--autosquash move commits that begin with squash!/fixup! under -i
-S, --gpg-sign[=<key-id>]
GPG-sign commits
--autostash automatically stash/stash pop before and after
-x, **--exec <exec> add exec lines after each commit of the editable list**
-r, --rebase-merges[=<mode>]
try to rebase merges instead of skipping them
--fork-point use 'merge-base --fork-point' to refine upstream
-s, --strategy <strategy>
use the given merge strategy
-X, --strategy-option <option>
pass the argument through to the merge strategy
--root rebase all reachable commits up to the root(s)
--reschedule-failed-exec
automatically re-schedule any `exec` that fails
--reapply-cherry-picks
apply all changes, even those already present upstream
# 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,reword,edit,squash,fixup,exec
2.1准備工作,初始化倉庫,添加文件,提交,基於master分支創建rebase_i分支
2.2pick 更改提交順序、刪除提交
2.2.1假定,我們現在要改變提交 M4.txt 和 M3.txt 的順序,該怎么操作?
2.2.1.1M3.txt是倒數第二次提交,告訴git 我要改變倒數第2次后的提交
$git rebase -i HEAD~2
接着,git給你一個文本,告訴你我知道了,下面注釋的是提示,我們不需要管,只要專注前兩行就ok
2.2.1.2把第一行和第二行交換順序.
接着 Esc,:wq 保存退出
2.2.1.3git log 查看更改
2.2.2假定,我們現在要刪除某一個提交(假設是M4),該怎么操作?
2.2.1.1M4.txt是排序后的倒數第二次提交,告訴git我要改變倒數第2次后的提交
$ git rebase -i HEAD~2
2.2.2.2刪除M4的信息:pick 14929f1 添加了一個M4.txt
11.2.2.3git log 查看更改
2.3record 修改提交消息(提交內容不變)
2.3.1假定,我們現在要修改某個提交的消息(M2),該怎么操作?
2.3.1.1git log 查看 M2 距離 HEAD 有多少的距離,得:2
git rebase -i HEAD~2
2.3.1.2只需要修改M2 ,pick 為 r 是 record簡寫
2.3.1.3接着 Esc,:wq 保存退出
2.3.1.4git會說 開始執行,接着彈出一個編輯窗口
接着 Esc,:wq 保存退出
2.3.1.5git log 查看, 消息 “添加了一個M2.txt” 變為了 “重新修改commitM2,添加了一個M2.txt”
【此種操作順帶的也會改變commit ID號】
2.4edit修改提交
2.4.1假定 我想要在兩個提交之間 再加提交,怎么辦?
2.4.1.1假定,我們要在 M2 和 M3之間 添加一條提交
git rebase -i HEAD~2
2.4.1.2只需要修改M2 ,pick 為 e 是 edit簡寫
2.4.1.3rebase_i分支多了REBASE-i 1/2
2.4.1.4查看當前變基操作的狀態
2.4.1.5給M2.txt 增加一些內容,然后提交
2.4.1.6提交完成之后,查看當前的狀態,並根據提供的選擇進行操作(演示選擇:git rebase --continue)
#使用“git rebase——edit-todo”查看和編輯
use "git rebase --edit-todo" to view and edit
You are currently editing a commit while rebasing branch 'rebase_i' on '9119c19'.
#您當前正在編輯提交,同時重新建立分支
#使用"git commit——amend"修改當前提交
use "git commit --amend" to amend the current commit
#當你對你的更改感到滿意時,使用“git rebase——continue”
use "git rebase --continue" once you are satisfied with your changes
2.4.1.7查看當前提交日志是否滿足需求
2.5.1假定 我想要單純的修改這次提交內容和消息,怎么辦?
可參見上述操作步驟進展至11.4.1.3rebase_i分支多了REBASE-i 1/2,11.4.1.6使用"git commit——amend"修改當前提交
2.5.1.1使用"git commit——amend"修改當前提交
2.5.1.2進入修改界面,完成修改后 按ESC,:wq
2.5.1.3繼續進行變基操作
$ git rebase --continue
2.5.1.4查看log
2.6.1假定,我想合並某幾個提交,怎么辦?(squash合並提交)
2.6.1.1合並 M1 和 M2
修改為
開始執行變更然后 在彈出來的編輯框里 寫提交信息,可以修改提交消息,默認是把兩個消息都合並
接着 Esc,:wq 保存退出,git log查看,合並成功
======================================================================
創作不易,本人熱衷開源共享 《git rebase(變基)操作演示》
======================================================================