git clone指定历史提交数量

我们在刚开始学习 git 时,可能学会的第一个命令就是 git clone。随着越来越多的使用 git,越发觉得它的功能非常强大,每个常用的 git 命令都值得我们用 git xxx --help 去学习一下。

平时在使用 git clone 时,可能会面临源仓库太大,而下载太慢的问题。其实,如果我们只是想学习一下其中的源码,那么可以在 git clone 时指定历史提交数量,这样会加快 clone 到本地的速度。

例如,使用 git clone下载 github 上 edk2 项目的源码,如果不指定历史提交数量,则需要下载 349834 个对象

$ git clone https://github.com/tianocore/edk2.git
正克隆到 'edk2'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (8/8), done.
^C收对象中:   0% (1/349834)

而如果指定 --depth=1000,则只需要下载 9481 个对象

$ git clone https://github.com/tianocore/edk2.git --depth=1000
正克隆到 'edk2'...
remote: Enumerating objects: 16134, done.
remote: Counting objects: 100% (16134/16134), done.
^Cmote: Compressing objects:  25% (2371/9481)

通过指定 --depth 可以减少 clone 的内容,实现将源码更快的下载到本地。

不过,这样做会带来一些坑,因为如果在 git clone 时不指定远程分支的话,默认只拉取 master 分支,这就导致 clone 下来的仓库里面其他远程分支和 tag 不可见,进而无法跟踪和 fetch 那些分支,这就需要我们在 clone 时指定想要的某个远程分支,例如:

$ git clone --depth=5 https://github.com/choumin/git-test.git -b cw1 git-test

如果已经通过 --depth=n 的方式 clone 到本地了,又不想重新 clone,可以通过下面的方法将其他远程分支拉取到本地:

$ git remote set-branches origin 'remote_branch_name'
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值