0%

在 git clone 时添加了 --depth 后,如何重新拉取所有全部的历史来获取完整的仓库

现象

由于众所周知的原因,国内在拉 https://github.com/xxx/xxx.git 的时候速度特别慢,所以呢吗,有时我们为了加快 clone 的速度会使用 –depth 参数,比如:

1
git clone https://github.com/xxx/xxx.git --depth 1

但是这么拉下来之后呢,会有一个问题,就是执行 git log 的时候会显示只有一条提交记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
root@d8acdd68f269:/source_code/mesos# git log
commit 54227a33b68d83b97ef1a7c14283351d45322317 (grafted, HEAD -> master, origin/master, origin/HEAD)
Author: Andrei Sekretenko <asekretenko@apache.org>
Date: Fri Feb 28 22:39:54 2020 +0100

Got rid of passing Shared<> into `Subscribers::Subscriber::send(...)`.

Now that operator API events are authorized synchronously,
`Subscribers::Subscriber::send(...)` is no longer deferred,
and copying arguments of `Subscribers::send()` into `Shared`
becomes unnecessary.

Review: https://reviews.apache.org/r/72179

那么我们要如何把之前的历史重新再 pull 下来呢?

解决方案

实际上git fetch 专门有个参数,用来将浅克隆转换为完整克隆。

1
2
3
4
root@d8acdd68f269:/source_code/mesos# git fetch --help
...
--unshallow
Convert a shallow repository to a complete one, removing all the limitations imposed by shallow repositories.

所以,只要执行 git fetch --unshallow 就可以了,接下来就等待命令执行完成,完成后就恢复到完整克隆了。

1
2
3
4
5
root@d8acdd68f269:/source_code/mesos# git fetch --unshallow
remote: Enumerating objects: 152405, done.
remote: Counting objects: 100% (152405/152405), done.
remote: Compressing objects: 100% (34825/34825), done.
Receiving objects: 3% (4524/150778), 1.26 MiB | 762.00 KiB/s