git fetch, git pull与git rebase比较

时间:2025-05-16 11:03:43

fetch

git-fetch,从其他Git库的branch或tag或refs下载对象和引用到本地。

特性:

  • 可以同时操作多个Git库
  • 默认操作Git库origin
  • 操作后更新.git/FETCH_HEAD

 

使用格式:

  • git fetch [<options>] [<repository> [<refspec>…?]]
  • git fetch [<options>] <group>
  • git fetch --multiple [<options>] [(<repository> | <group>)…?]
  • git fetch --all [<options>]

示例:
git fetch,从默认远程库fetch所有内容
git fetch origin,从origin远程库fetch所有内容

 

pull

git-pull,从其他Git库的branch或tag或refs下载对象和引用,然后合并到当前分支。
特性:

 

  • git pull等价于git fetch, git merge FETCH_HEAD
  • git pull --rebase等价于git fetch, git rebase FETCH_HEAD

使用格式:

 

 

  • git pull [options] [<repository> [<refspec>…​]]


示例:
git pull,从默认远程库fetch所有内容,然后合并到当前分支
git pull origin,从origin远程库fetch所有内容,然后合并到当前分支
git pull origin remote_branch,从origin远程库的remote_branch分支fetch所有内容,然后合并到当前分支

 

 

 

 

rebase

git-rebase,在新基准的基础上,将本地基于旧基准的commits再做一遍。

特性:

  • 如果给出[<branch>],则执行git checkout <branch>
  • 相对于当前本地分支的upstream,保存本地分支的所有commits到临时工作区
  • 将当前分支reset到其对应的upstream,即执行git reset --hard <upstream>
  • 对当前分支,重新执行保存到临时工作区的所有commits。如果有冲突,则手工解决冲突后,执行git rebase --continue

使用格式:

 

 

  • git rebase [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>] [<upstream> [<branch>]]
  • git rebase [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
  • git rebase --continue | --skip | --abort | --quit | --edit-todo


示例:
当前分支为topic,执行git rebase master,等价于git rebase master topic。即采用最新的master作为基准,在此基础上将topic分支的本地修改再做一遍。