【git】git pull和git pull --rebase的区别和使用

使用下面的关系区别这两个操作:
git pull               = git fetch + git merge
git pull --rebase = git fetch + git rebase

git pull               =  git fetch + git merge FETCH_HEAD 
git pull --rebase =  git fetch + git rebase FETCH_HEAD 

差距就在git fetch之后的操作:

现在来看看git merge和git rebase的区别。

假设有3次提交A,B,C。

在远程分支origin的基础上创建一个名为"mywork"的分支(/本地分支)并提交了,同时有其他人在"origin"上做了一些修改并提交了。

其实这个时候E不应该提交,因为提交后会发生冲突。如何解决这些冲突呢?有以下两种方法:

1、git merge
用git pull命令把"origin"分支上的修改pull下来与本地提交合并(merge)成版本M,但这样会形成图中的菱形,让人很困惑。

如果你装了小乌龟,查看提交日志,可能就是下面的样子:

     D--------E
      /          \
 A---B---C---F----G---   test, master

2、git rebase
创建一个新的提交R,R的文件内容和上面M的一样,但我们将E提交废除,当它不存在(图中用虚线表示)。由于这种删除,E也push不到其他的repository.

rebase的好处是避免了菱形的产生,保持提交曲线为直线,让大家易于理解。

举例:

现在我们有这样的两个分支,test和master,提交如下:

       D---E test
      /
 A---B---C---F--- master

在master执行git merge test,然后会得到如下结果:

       D--------E
      /          \
 A---B---C---F----G---   test, master

在master执行git rebase test,然后得到如下结果:

A---B---D---E---C‘---F‘---   test, master


链接:https://www.jianshu.com/p/dc367c8dca8e
 

在rebase的过程中,有时也会有conflict,这时Git会停止rebase并让用户去解决冲突,解决完冲突后,用git add命令去更新这些内容,然后不用执行git-commit,直接执行git rebase --continue,这样git会继续apply余下的补丁。
在任何时候,都可以用git rebase --abort参数来终止rebase的行动,并且mywork分支会回到rebase开始前的状态。

参考或摘抄自:

简单对比git pull和git pull --rebase的使用 - 散尽浮华 - 博客园

https://www.jianshu.com/p/dc367c8dca8e

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/121507096