git-rebase进阶使用

本文详细介绍了Git rebase的四个高级用法,包括将当前分支变基到目标分支、变基到特定提交、对某一段提交进行变基以及在变基时保留合并提交。在使用前,建议对Git基础概念有一定了解,因为rebase操作涉及提交历史的修改,通常应在个人分支上执行。冲突处理是变基过程中可能遇到的问题,需要手动解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

01

02

>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>]

03
在这里插入图片描述

>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, 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值