git rebase
进阶使用
推荐书籍: 《git pro》
推荐教程: atlassian
本地获取帮助:
git help <command>
-
rebase
最强大的地方在于可以按需移动提交并对其进行编辑更改,当然前提是在个人分支上 -
最好对git中的一些概念预先有一定的了解
-
再次强调:
git rebase
相关命令只允许在个人分支上使用
目录:
- 本文alias说明
- 用法一:将当前分支的提交变基到目标分支
- 用法二:将当前分支变基到之前的提交, 重写其后的提交
- 用法三:将当前分支某一段提交变基到目标分支
- 用法四:变基时保留合并提交
- 冲突
alias
说明
git ll
=git log --oneline
git br
=git branch
git st
=git status
git ci
=git commit
git lh
=log --pretty=format:'%Cred%h %C(yellow)%ad%Creset %Cred%s%Creset %Cblue[%an] %C(yellow)%d' --graph --date=short --topo-order
用法一
将当前分支的提交变基到目标分支: git rebase <upstream> [<branch>]
-
<upstream>
:目标分支或新基点 -
如果提供
<branch>
,则会执行git checkout <branch>
,默认为HEAD
>git br
* master
test
>git ll master
1a17ba1 (HEAD -> master) add c
4428c06 add a
>git ll test
d691c94 (test) add b
4428c06 (HEAD -> master) add a
>git co test
>git rebase master (或 git rebase master test)
First, rewinding head to replay your work on top of it...
Applying: add b
>git br
master
* test
>git ll test
30cec3f (HEAD -> test) add b
1a17ba1 (master) add c
4428c06 add a
>git ll master
1a17ba1 (master) add c
4428c06 add a
可能的场景:
- 在将当前分支合并到主分支前执行变基操作,然后再合并到主分支,可以实现
fast-forward
用法二
将当前分支变基到之前的某一提交, 重写其后的提交:git rebase -i <upstream> [<branch>]
>git ll test
4a4633c (HEAD -> test) change d
f3ff56f add d
30cec3f add b
1a17ba1 (master) add c
4428c06 add a
> git rebase -i HEAD~2
>>pick f3ff56f add d
>>pick 4a4633c change d
//改为并保存:
>>pick f3ff56f add d
>>suqash 4a4633c change d
//输入提交信息并保存:
>>add and change d
[detached HEAD fb38e3f] add and change d
Date: Mon Jul 22 19:46:25 2019 +0800
1 file changed, 2 insertions(+)
create mode 100644 d
Successfully rebased and updated refs/heads/test.
>git ll test
fb38e3f (HEAD -> test) add and change d
30cec3f add b
1a17ba1 (master) add c
4428c06 add a
交互式rebase
的选项说明:
pick f3ff56f add d
pick 4a4633c change d
# Rebase 30cec3f..4a4633c onto 30cec3f (2 commands) //表示将30cec3f..4a4633c(左开右闭)变基到30cec3f,包含2条命令,即有两个提交需要处理
#
# Commands:
# p, pick = use commit //表示正常使用该提交
# r, reword = use commit, but edit the commit message //正常使用该提交,但会提示修改提交信息
# e, edit = use commit, but stop for amending //正常使用该提交内容,但提交后会停下,此时可以执行git commit --amend或者其他命令,最后git rebase --continue
# s, squash = use commit, but meld into previous commit //与前一提交合并,会提示输入提交信息
# f,