git rebase使用场景

1. 当前分支落后拉取后,整理commit,使得提交历史为直线html

git pull = git fetch + git mergegit

git pull --rebase = git fetch + git rebaseide

其实--rebase的目的只有两个:fetch

1.让多我的在同一个分支开发的提交节点造成一条线,而不是多条线spa

2.让你提交的commit在该分支的最前面.net

当push被reject的时候,能够用 git pull --rebase 拉取内容,将提交记录保持一条直线code

git pull --rebase时候,不能有modified状态的文件,能够有Untracked files。modified状态的文件,能够commit或者stash一下,一般的操做有:htm

$ git stash
$ git pull --rebase
$ git push
$ git stash pop

2. 别的分支合并到master,先整理为直线,再合并blog

1. 开辟特性分支 git checkbox -b feature
2. 在feature分支提交commit
3. 在feature git reabse -i 合并多个commit为一个,简化提交历史。这样能够在git rebase时候减小冲突解决次数。
4. 在feature git pull origin master --rebase (至关于git fetch origin master, git rebase origin/master)
                                                                    将master最新commit同步到此分支,可能要手动解决冲突(合并过程当中,异常退出vi窗口,
                                                                    用git rebase --edit-todo恢复,修改完,git rebase --continue 继续下一步,
                                                                   忽略,git rebase --skip,终止,用git rebase —abort)
5. 切回master,git merge feature 将feature分支内容合并到master
6. git push提交master排序

如此,master的提交历史将是一条直线。

3. 分支的多个连续commit复制到其余分支

git rebase [startpoint] [endpoint] --onto [branchName] //将多个连续commit添加到目标分支branchName, [startpoint] [endpoint]指定的是一个 前开后闭的区间

例如:

git rebase 90bc0045b^ 5de0da9f2 --onto master  //90bc0045b^后退一个commit,作成一个[90bc0045b, 5de0da9f2]闭区间

rebase完后,须要

git reset --hard commitId //将分支的HEAD指向提交的id

4. commit合并

git rebase -i [startpoint] [endpoint] //[endpoint]可省略,将多个commit合并为一个,使用squash模式

例如:

git rebase -i HEAD~3 //往前3个commit,到如今,进行合并

5. commit拆分

git rebase -i 对指定commit的pick改成edit,进行编辑拆分

6. commit排序

git rebase -i 修改列表中commit顺序便可

7. 改变分支起点

git checkout feature2

git rebase master

能够将feature2起点从别的分支改到master分支上

 

注意点:

主分支上不能rebase,若是如此,主分支的历史将被篡改,不能看到原始的历史记录了

git pull时能够加上--rebase参数, 使之不产生Merge点, 保证了代码的整洁,每次加rebase参数有点麻烦,

给指定分支设置为rebase方式,可使用以下方式:

 $ git config branch.dev.rebase true

使用时将 "dev" 修改为您本身本地的分支名字,必须cd到工程目录下,才能更改分支配置

全部的分支都应该用rebase

$ git config --global branch.autosetuprebase always

这样新建的分支会设置为rebase,已经建好的还需单独设置

 

参考:

https://baijiahao.baidu.com/s?id=1633418495146592435&wfr=spider&for=pchttps://www.liaoxuefeng.com/wiki/896043488029600/1216289527823648https://blog.csdn.net/nrsc272420199/article/details/85555911https://www.codercto.com/a/45325.html

相关文章
相关标签/搜索