2. git rebase的基本使用

git rebase:改变基底。下面举例说明git rebase的作用:

A---B   remote master
A---B   local master

如上图,首先通过git pull同步远程的master分支。
然后我新建了一个feature分支用于开发相关的功能,如下图:

A---B   remote master
A---B   local master
	 \
	  C---D   feature

我在feature上实现功能时,有人在远程上传了新的提交,如下:

A---B---E   remote master
A---B   local master
	 \
	  C---D   feature

此时如果我需要合并feature到master,我需要回到master分支去做git pull操作,从而同步远程的master,如下:

A---B---E   remote master
A---B---E   local master
	 \
	  C---D   feature

然后我回到feature上,使用git rebase master改变feature的基底,结果如下:

A---B---E   remote master
A---B---E   local master
		 \
	 	  C---D   feature

将分支feature的基底改为E的过程中,会进行冲突检查。假设C和E有冲突,处理冲突的过程如下:

1.对冲突文件进行修改
2.git add .
3.git commit -m"处理C和E的冲突"
4.git rebase --continue # 这个命令表示继续进行rebase,如果上面刚提交的版本"处理C和E的冲突"与D有冲突,那么就会回到步骤1

在进行完rebase操作以后,最后我们一般在master中执行git merge feature来实现合并,结果如下:

A---B---E   remote master
A---B---E--------F   local master
		 \     /
	 	  C---D   feature

上面的这个使用merge和rebase的过程时官网推荐的,即
第一步,次级分支rebase main
第二步, main merge次级分支

git rebase -i master 可以进行交互式的rebase操作【暂时不学,以后再说】

参考:
https://www.bilibili.com/video/BV19B4y1u7vm
https://www.bilibili.com/video/BV1Xb4y1773F
https://www.csdn.net/tags/Mtjacg3sMDMyMjUtYmxvZwO0O0OO0O0O.html

posted @ 2022-07-26 19:51  好人~  阅读(535)  评论(0)    收藏  举报