TL; DR

git pull 等价于 fetch + merge,会产生 merge commit

git pull --rebase 等价于 fetch + rebase,会把本地提交重新应用到远程分支后面,从而保持提交历史线性

1. git pull 本质是什么

在 Git 中,git pull,实际上等价于:git fetch && git merge

流程:

  1. fetch:从远程仓库下载最新 commit
  2. merge:把远程分支 merge 到当前分支

例如远程仓库:

A---B---C  origin/main

本地:

A---B---D  main

执行 git pull,结果:

A---B---C
     \  \
      D--M

出现:

M = merge commit

特点:

  • 会产生 merge commit
  • 历史会出现 分叉结构

2. git pull --rebase

执行 git pull --rebase,等价于:

git fetch
git rebase origin/main

流程:

  1. fetch 最新代码
  2. 把你本地 commit 拿下来
  3. 放到远程最新 commit 后面

例如远程:

A---B---C  origin/main

本地:

A---B---D  main

执行:

git pull --rebase

结果:

A---B---C---D'

注意:

  • D' 是新的 commit
  • 不会产生 merge commit
  • 历史是 线性的

3. 直观对比

git pullgit pull —rebase
本质fetch + mergefetch + rebase
commit 历史分叉线性
是否产生 merge commit✅ 会❌ 不会
是否改变 commit hash❌ 不会✅ 会

4. 为什么很多人推荐 pull --rebase

因为历史更干净,例如:

使用 pull

A
B
C
 \ 
  D
   \
    M

使用 pull —rebase

A
B
C
D

在大型项目(如 LLVM)里,线性历史更容易 review 和 debug

5. 实际开发建议

很多开发者直接设置默认 git config --global pull.rebase true

git pull 就等价于 git pull --rebase