git pull、git fetch、git merge 与 git rebase

参考文档:

  1. Git - git-fetch Documentation
  2. Git - git-pull Documentation
  3. Git - git-rebase Documentation

git pull 与 git fetch 的功能大致相同,起到更新代码的作用。

git fetch

git fetch 获取远端仓库分支,更新git远程追踪的分支,不进行合并。

git pull

git pull官方文档描述:

Incorporates changes from a remote repository into the current branch. If the current branch is behind the remote, then by default it will fast-forward the current branch to match the remote. If the current branch and the remote have diverged, the user needs to specify how to reconcile the divergent branches with –rebase or –no-rebase (or the corresponding configuration option in pull.rebase).

合并远端仓库到当前分支,如果当前在远程分支之后,默认采用fast-forward(快进)合并。入伙当前分支和远程分支存在在分歧,用户需要通过参数指定如何合并分支(rebase或者no-rebase)。

More precisely, git pull runs git fetch with the given parameters and then depending on configuration options or command line flags, will call either git rebase or git merge to reconcile diverging branches.

更具体的说,git pull 根据给定的参数运行 git fetch,然后根据参数或者命令行标识调用git rebase 或者 git merge合并分支。

默认的操作为git merge。

假设有如下分支:

1
2
3
4
     A---B---C master on origin
/
D---E---F---G feature
^ origin/master in your repository

git pull 会获取并重现从分歧处开始的远程分支中的修改,直到当前提交(节点C)在master的顶部并将结果记录在一个新的提交中。git merge后的结果如下:

1
2
3
4
git merge: 
A---B---C origin/master
/ \
D---E---F---G---H feature

git rebase将当前分支的所有未保存在<upstream>中commit点保存到的临时区域,然后把当前分支更新到最新的原分支,并将保存的提交一个个按序添加到当前分支的顶部。

1
2
3
4
5
git rebase:
A---B---C'--F'--G' feature
/
D---E
^ origin/master in your repository

git pull、git fetch、git merge 与 git rebase
https://ww1820.github.io/posts/27e3c6f0/
作者
AWei
发布于
2023年5月11日
更新于
2023年5月11日
许可协议