Git——rebase命令

1. 应用场景

  • 合并多次提交记录
  • 分支合并
  • 对一个分支做『变基』操作

2. 合并多次提交记录

合并最近两次commit:

git rebase -i HEAD~2

执行命令后会自动进入 vi 编辑模式:

pick 6935383 rename冲突
pick 23b1a51 debug

# Rebase eb7f366..23b1a51 onto eb7f366 (2 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.

【注】

  • p, pick = use commit
  • r, reword = use commit, but edit the commit message
  • e, edit = use commit, but stop for amending
  • s, squash = use commit, but meld into previous commit
  • f, fixup = like “squash”, but discard this commit’s log message
  • x, exec = run command (the rest of the line) using shell
  • d, drop = remove commit

修改提交纪录并保存退出。

pick 6935383 rename冲突
s 23b1a51 debug

保存后到注释修改界面。

# This is a combination of 2 commits.
# This is the 1st commit message:

rename冲突

# This is the commit message #2:

debug

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Nov 24 22:56:02 2020 +0800
#
# interactive rebase in progress; onto eb7f366
# Last commands done (2 commands done):
#    pick 6935383 rename冲突
#    squash 23b1a51 debug
# No commands remaining.
# You are currently rebasing branch 'maseter' on 'eb7f366'.

(非编辑状态输入dd删除一行)

保存后即合并完成。

异常情况:

1、不要合并已提交远程分支的纪录

如果这样做,可能出现push rejected。 解决方式当然是先拉下远程的代码,进行冲突处理,再进行提交。

2、git rebase -i 异常操作导致退出了vim编辑页面,会提示:

$ git rebase -i head~2
error: could not apply 040bd4b... commit on issue-005
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 040bd4b... commit on issue-005
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md

使用 git rebase --edit-todo 会再次进入刚才编辑错误退出前的vim状态,这时候可以修改你的编辑。

使用git rebase --abort  表明退出当前的合并请求( 又回到原来的2个commit的状态)

3. 变基

使用场景:本地与远端同一分支提交历史不一致

多个人在同一个分支上协作时,比如由我和A一同开发。我在修复了一个bug以后准备提交。

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git add .

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git commit -m "debug the world"
[master a40d43c] debug the world
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aa.txt

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git push origin master
To github.com:Zhangtao153/learngit.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'github.com:Zhangtao153/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

push失败了,说明A在我之前已经提交了,我本地master分支的提交历史已经落后远端了,需要先pull一下,与远端同步后才能push

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git pull
Merge made by the 'recursive' strategy.
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git log --oneline --graph
*   912bb85 (HEAD -> master) Merge branch 'master' of github.com:Zhangtao153/learngit into master
|\
| * 7ad4fd6 (origin/master) add a new function
* | a40d43c debug the world
|/
* a8d2e0d 手动删除
* 1d243ca add test file
* 8438389 wrote a readme file

竟然分叉了!由于我本地master的提交历史和远端的master分支的提交历史不一致,所以git为我进行了自动合并,然后生成了一个新的提交历史。

这个时候就可以用 git rebase 解决分叉的问题。

$ git rebase
Successfully rebased and updated refs/heads/master.

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git log --oneline --graph
* 4178b6e (HEAD -> master) debug the world
* 7ad4fd6 (origin/master) add a new function
* a8d2e0d 手动删除
* 1d243ca add test file
* 8438389 wrote a readme file

然后再push,将本地修改同步到远端。

git pull --rebase 和上面的效果一致。

4. 合并分支

先创建一个分支用于解决bug

$ git checkout -b issues-001
Switched to a new branch 'issues-001'

接下解决bug,然后保存提交

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
$ git add .

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
$ git commit -m "issues-001"
[issues-001 7a55a26] issues-001
 1 file changed, 1 insertion(+)

先尝试通过 merge 合并:

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git merge issues-001
Merge made by the 'recursive' strategy.
 aa.txt | 1 +
 1 file changed, 1 insertion(+)

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git log --oneline --graph
*   ae02ea0 (HEAD -> master) Merge branch 'issues-001' into master
|\
| * 7a55a26 (issues-001) issues-001
* | d36d166 commit on master
|/
* 4178b6e (origin/master) debug the world
* 7ad4fd6 add a new function
* a8d2e0d 手动删除
* 1d243ca add test file
* 8438389 wrote a readme file

虽然合并成功,但是Master已经保存了合并历史,出现开叉了!

通过rebase合并分支

先将代码回退到merge之前

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git reset --hard HEAD^
HEAD is now at d36d166 commit on master

先切换回issues-001分支,在issues-001分支上执行: git rebase master

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git switch issues-001
Switched to branch 'issues-001'

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
$ git rebase master
Successfully rebased and updated refs/heads/issues-001.

【注】如果rebase出现冲突,先解决冲突,然后通过add添加,之前的rebase其实只是完成了一半,由于出现冲突而终止,冲突解决之后,可以通过git rebase —continue继续完成之前的rebase操作。

切换到主分支master,将issues-001分支上的提交合并过来。

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git merge issues-001
Updating d36d166..43823d4
Fast-forward
 aa.txt | 1 +
 1 file changed, 1 insertion(+)

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git log --oneline --graph
* 43823d4 (HEAD -> master, issues-001) issues-001
* d36d166 commit on master
* 4178b6e (origin/master) debug the world
* 7ad4fd6 add a new function
* a8d2e0d 手动删除
* 1d243ca add test file
* 8438389 wrote a readme file

master是一条直线了。最后删除掉issues-001分支。

Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
$ git branch -d issues-001
Deleted branch issues-001 (was 43823d4).

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值