美文网首页
Git Rebase使用方法

Git Rebase使用方法

作者: 千载不变灬 | 来源:发表于2019-09-30 20:26 被阅读0次

Git Rebase有两种使用场景:
一、对本地分支代码多次commit进行合并
二、对本地分支代码进行变基操作,将其他分支commit合入到当前分支

一、合并commit记录:
在工作中,有些同学的习惯是将每天编写的代码进行提交,这样本地就会有多次commit记录,如果提交到主库上就对它的commit记录造成了污染。

步骤:
(1)获取提交记录,确认需要合并最近3条记录
$ git log --pretty=oneline -4
auiregfq8234g0923h09fdadfoiasdfhoiasdhf03f2oia Req:333
asdfasdaqw3f212543512345231234123gwrgwreg Req:222
34trgw354t2gdfwegq34t234t23gwegrwergwetg234 Req:111
234t23ggeegr34trgw35rgwetg23dfwegq34tw4t2w4 BUG:fix xxx

(2)合并最近三次提交
$ git rebase -i HEAD~3

提交后会进入编辑模式,注意下,这里的顺序会跟git log查出来相反。

pick 34trgw Req:111
pick asdfas Req:222
pick auireg Req:333


# Rebase 5f2452b2..8f33126c onto 5f2452b2 (4 commands)
#
# Commands:
# 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
#
# 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.
#

(3)将后面两个pick改为s(这样做的效果就是222和333的代码合并到111这次commit中)

pick 34trgw Req:111
s asdfas Req:222
s auireg Req:333

(4)然后保存退出,会进入另外一个编辑器中,这时可以对commit信息进行修改(把Req:111改为Req:new info),再保存退出

# This is a combination of 3 commits.
# The first commit's message is:
Req:111

# This is the 2nd commit message:
Req:222

# This is the 3rd commit message:
Req:333

(5)rebase完成,再用$ git log --pretty=oneline -2查看得到:

34trgw354t2gdfwegq34t234t23gwegrwergwetg234 Req:new info
234t23ggeegr34trgw35rgwetg23dfwegq34tw4t2w4 BUG:fix xxx

二、分支变基:


image.png

1.先从master上chckout出一个分支;
2.master和feature各自演进;
3.feature准备合入master。

以前我是用git reset --soft退回master记录,再本地stash,pull完代码后,再进行unstash。
现在就可以直接在feature上执行rebase操作来代替上述操作:
1.git checkout master
2.git pull origin master
3.git checkout feature
4.git rebase master

第四步rebase干了哪些事呢?
先将master分支的代码checkout,存到工作目录下,然后将feature分支的commit依次合并。
如果合并的过程没问题,也就是没有冲突,那rebase到这里就算完成。

如果发生了冲突的情况,就会报错error:Failed to merge in the changes.
自行解决冲突完毕后,使用git add .命令添加修改文件,然后使用git rebase –continue完成整个rebase过程。

如果你不想处理冲突,有两种方式恢复:
一、使用git rebase –abort放弃此次操作,分支会回到 rebase 开始前的状态。
二、使用git rebase –skip,用master分支的取代当前feature分支。

注:git rebase会导致其他开发的feature分支和你的feature出现不一样的历史,因此在多人协同开发同个分支的情况下需要谨慎使用

相关文章

  • git8~rebase

    2019.06.25 git rebase git stash git pull --rebase git sta...

  • 如何正确rebase

    git rebase -i origin/分支 合并冲突 git status git rebase --cont...

  • Git Rebase使用方法

    Git Rebase有两种使用场景:一、对本地分支代码多次commit进行合并二、对本地分支代码进行变基操作,将其...

  • git整体学习

    基础 1. git ... 3. git rebase 第二种合并分支的方法是 git rebase。Rebase...

  • Git错误信息解决记录

    Git error: previous rebase directory .git/rebase-apply st...

  • Git使用教程

    拉取代码的正确步骤 git fetch git rebase git rebase --continue(遇到冲突...

  • Git 批量合并Commit或commit信息

    git rebase -i HEAD~3 git rebase -i [commitId-a] [commitId...

  • git merge conflict

    git pull --rebase编辑confilct的文件,修改完毕后git addgit rebase --c...

  • git rebase 用法

    git rebase Git rebase 与 Git merge 的区别 如果经常多人协作开发的话,可能都很熟悉...

  • 【Git】rebase 用法小结

    本文主要参考https://git-scm.com/docs/git-rebase rebase在git中是一个非...

网友评论

      本文标题:Git Rebase使用方法

      本文链接:https://www.haomeiwen.com/subject/iyfzyctx.html