合并代码还在用 git merge 吗?不妨试试 git rebase
点击下方“IT牧场”,选择“设为星标”
git merge 和 git rebase 目的都是将一个分支的 commit 合并到到另外一个分支中去,但两者有一定区别。
git merge





如果此时我push本地的提交到远程,就会被拒绝,因为远程和本地已经各自有commit了,我们常规的做法是git pull一下,在本地解决冲突,然后继续push,本质上git pull = git fetch + git merge
。
产生冲突:


处理冲突:

重新走add commit 然后push,可以看到必须将合并当作一个新的commit:

git rebase
如果我们此时采用git pull --rebase,也就是=git fetch + git rebase
。



git rebase


总结
merge 是一个合并操作,会将两个分支的修改合并在一起,默认操作的情况下会提交合并中修改的内容。 merge 的提交历史记录了实际发生过什么,关注点在真实的提交历史上面。 rebase 并没有进行合并操作,只是提取了当前分支的修改,将其复制在了目标分支的最新提交后面。 rebase 操作会丢弃当前分支已提交的 commit,故不要在已经 push 到远程,和其他人正在协作开发的分支上执行 rebase 操作。 merge 与 rebase 都是很好的分支合并命令,没有好坏之分,使用哪一个应由团队的实际开发需求及场景决定。 如果比较关注commit时间的话,还是用git merge,rebase会打乱时间线是不可避免的。
干货分享
最近将个人学习笔记整理成册,使用PDF分享。关注我,回复如下代码,即可获得百度盘地址,无套路领取!
•001:《Java并发与高并发解决方案》学习笔记;•002:《深入JVM内核——原理、诊断与优化》学习笔记;•003:《Java面试宝典》•004:《Docker开源书》•005:《Kubernetes开源书》•006:《DDD速成(领域驱动设计速成)》•007:全部•008:加技术群讨论
加个关注不迷路
喜欢就点个"在看"呗^_^
评论