1. 当前分支落后拉取后,整理commit,使得提交历史为直线
git pull
git log //出现分叉,进行整理
git rebase //将commit移动位置,把pull过来的commit放前面,为提交的commit放后面,成一条直线
2. 别的分支合并到master,先整理为直线,再合并
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分支上
参考:
https://baijiahao.baidu.com/s?id=1633418495146592435&wfr=spider&for=pc
https://www.liaoxuefeng.com/wiki/896043488029600/1216289527823648
https://blog.csdn.net/nrsc272420199/article/details/85555911
https://www.codercto.com/a/45325.html