Git rebase操作

来源:互联网 发布:java设计模式书籍知乎 编辑:程序博客网 时间:2024/05/21 22:52

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.1git rebase -I

 

弹出编辑器,内容如下

 

pickE注释

pickF注释

pickG注释

 

#RebaseD..Gonto 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

 

说明:

pickE注释

pickF注释

pickG注释

 

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

 

pick:不做任何修改;

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

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

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

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

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

 

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

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

如果有rewordsquash的操作,会弹出编辑提交注释,修改后保存退出,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