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(变基)操作演示》
======================================================================