Git rebase操作

1git rebae -I

 

说明:对单个分支做rebase,将当前分支未push到远程服务器的提交做整理,就像把这部分提交拿出来逐条整理一遍,可以修改提交的文件,做增补提交,可以修改提交注释,可以合并到上一条提交,可以删除某条提交等。

 

图示:

 

                     E---F---G       分支A

                   /

A---B---C---D    origin/分支A

 

 

从图示上看到分支A在本地有EFG  3条提交没有push到远程服务器,现在在分支A上用git rebase -i就可以对这3条提交做整理

 

命令:

 

1.1 git rebase -I

 

弹出编辑器,内容如下

 

pick E 注释

pick F 注释

pick G 注释

 

#Rebase D..G onto D

#

# Commands:

#  p, pick = use commit

#  r, reword = use commit, but edit the commitmessage

#  e, edit = use commit, but stop for amending

#  s, squash = use commit, but meld intoprevious commit

#  f, fixup = like "squash", butdiscard this commit's log message

#  x, exec = run command (the rest of the line)using shell

#

# These lines can bere-ordered; they are executed from top to botto

#

# If you remove aline here THAT COMMIT WILL BE LOST.

#

# However, if youremove everything, the rebase will be aborted.

#

# Note that emptycommits are commented out

 

说明:

pick E 注释

pick F 注释

pick G 注释

 

这部分的pick 是可以编辑修改的,如编辑内容所示,可以改成pickrewordeditsquashfixupexec

 

pick :不做任何修改;

reword:只修改提交注释信息;

edit:修改提交的文件,做增补提交;

squash:将该条提交合并到上一条提交,提交注释也一并合并;

fixup:将该条提交合并到上一条提交,废弃该条提交的注释;

如果删除了某行记录,比如删除“pick G 注释”,这条提交也会被删除。

 

1.2 编辑完成后,保存退出;

1.3 如果有edit的操作,git会新建一个rebase分支,将提交resetedit的这条记录,做增补提交之后,再用 git rebase --continue git 继续执行rebase,成功之后回到分支A

如果有reword squash的操作,会弹出编辑提交注释,修改后保存退出,git 自动继续执行rebase

其他的命令,git rebase会自动执行。

 

 

2git rebase 分支A 分支B

 

说明:将分支B变基到分支A的最新版本上,如果是分支B留空则是将当前分支变基到分支A的最新版本上

 

图示:

 

                     E---F---G       分支B

                   /

A---B---C---D---H---I---J    分支A

 

命令:git rebase 分支A 分支B    或者在分支B上用    git rebase 分支A    结果一样

 

结果:

 

                                      E'---F'---G'       分支B

                                     /

A---B---C---D---H---I---J    分支A

 

 

3git rebase  --onto 分支分支B 分支C

 

说明:这个操作是想将分支C变基到分支A的最新版本上,而分支C和分支A是通过分支B间接关联起来的。

 

图示:

                               K---L---M  分支C

                              /

                     E---F---G       分支B

                    /

A---B---C---D---H---I---J    分支A

 

命令:git rebase  --onto 分支分支B 分支C

 

结果:

                       E---F---G       分支B

                      /                 K'---L'---M ' 分支C

                     /                /

A---B---C---D---H---I---J    分支A

 

 

分支C和分支B的关系去掉了,但是他们之间共同修改过的文件,分支B上的修改的也会加到分支C上。

 

注:如果是跳过分支B,用命令git rebase --onto 分支分支C会出现这个错误

First,rewinding head to replay your work on top of it...

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值