Git - rebase

147 阅读3分钟

1、将多个commit合并成一条commit记录

    • git rebase -i [startpoint] [endpoint]
    • git rebase -i HEAD~3
    • i :即弹出交互式的界面让用户编辑完成合并操作
    • [startpoint] [endpoint] :则指定了一个编辑区间
  • 执行这个命令后会跳到一个vi编辑器(提示:)

    • pick:保留该commit(缩写:p)
    • reword:保留该commit,但我需要修改该commit的注释(缩写:r)
    • edit:保留该commit, 但我要停下来修改该提交(不仅仅修改注释)(缩写:e)
    • squash:将该commit和前一个commit合并(缩写:s)
    • fixup:将该commit和前一个commit合并,但我不要保留该提交的注释信息(缩写:f)
    • exec:执行shell命令(缩写:x)
    • drop:我要丢弃该commit(缩写:d)
  • VIM操作:退出操作

    • 按【ESC】键跳到命令模式,
    • 然后再按【:】冒号键
    • 最后再按【wq】
    • 即可保存退出vi的编辑状态
    • 如果是不想保持直接按下【:】冒号键加【q!】键,就能直接推出;
// 合并从当前head到15f745b(commit id)
git rebase -i 15f745b

// 1、合并最近的两次提交( 弹出操作页面 )
git rebase -i HEAD~2

// 2、按照意愿直接操作commit记录( 直接操作前几行要处理的commit )
例如:
s b7ca69c dev第三次提交
s 7ff3f9e dev第四次提交

// 3、保存第二部的操作 : ESC 、: 、wq
此时如果有报错:error: cannot 'squash' without a previous commit
步骤一:git rebase --edit-todo
步骤二:git rebase --continue


// 4、在This is the commit message #2 处,修改当前合并后的 注释

// 5、保存第4部的操作 : ESC 、: 、wq

// 6、退出 : ESC 、: 、q!


// 7、切换到当前分支 : git checkout dev


// 8、切换到当前分支 : git reset --hard  50a6c5d3
7a6454e8

2、把 dev 的变更 rebase 到当前 master 分支上

  • git checkout master
  • git rebase [startpoint] [endpoint]
  • git rebase --continue :// 解决冲突
  • 此时会生成一个变基过程的游离指针域 :"e0d9b82"
  • git checkout master
  • git reset --hard "e0d9b82"

3、把当前 dev 分支的变更 rebase 到 master 分支上

  • git checkout dev
  • git rebase b67d430358 172f72e236 b67d430358 --onto master
  • git rebase --continue :// 解决冲突
  • 此时会生成一个变基过程的游离指针域 :"e0d9b82"
  • git checkout master
  • git reset --hard "e0d9b82"

4、冲突、跳过、撤销 rebase

a、 git pull --rebase:重新定义(re)起点(base)
  • 作用:将本地当前分支里的每个提交(commit)取消掉,然后把将本地当前分支更新为远程最新的"origin"分支
  • 等同于 git fetch + git rebase
    • git fetch: 从远程获取最新版本到本地,不会自动合并分支
    • git rebase: 重新定义(re)起点(base)的作用,即重新定义分支的版本库状态
  • 遇到冲突使用 git rebase --abort、git rebase --skip、git rebase --continue 解决
b、 git rebase --abort:撤销 rebase
  • 执行之后,本地内容会回到提交之间的状态
  • 就是回到以前提交但没有pull是的状态
  • 简单来说就是撤销rebase
c、 git rebase --skip:跳过冲突
  • 引起冲突的 commits 会被丢弃
d、git rebase --continue:合并冲突
  • 执行完$git pull --rebase 之后,本地如果产生冲突
  • 手动解决冲突之后,用"git add"命令去更新这些内容的索引(index)