git fetch, git pull与git rebase比较

本文介绍了git fetch、git pull和git rebase三个命令的区别和使用场景。git fetch用于从远程仓库获取更新,不合并到本地;git pull则将远程更新合并到本地;git rebase则是将本地提交基于远程更新重放,常用于保持提交历史的整洁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.git 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所有内容

 

2.git 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所有内容,然后合并到当前分支

 

 

 

 

3.git 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分支的本地修改再做一遍。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值