to inspire confidence in somebody.

0%

Git的浅克隆功能

随着Git仓库不断的被修改,整个仓库会变得越来越大,其中最主要的原因是历史提交特别的多,这个对于想立即阅读最新代码或者CI/CD场景下不是特别友好。

面对这种场景,可以利用git提供的浅克隆功能,只clone少部分历史到本地,这样可以极大的减少clone的仓库大小,以PHP源代码代码为例:

目前PHP源码有超过10万个提交,全部clone下来成本不小,如果只是想clone最新的N个提交,可以加上--depth参数:

1
# git clone https://github.com/php/php-src.git --depth=1 # 1可以换成任意深度,根据需要调整
2
Cloning into 'php-src'...
3
remote: Enumerating objects: 18674, done.
4
remote: Counting objects: 100% (18674/18674), done.
5
remote: Compressing objects: 100% (16621/16621), done.
6
remote: Total 18674 (delta 2656), reused 12214 (delta 1882), pack-reused 0
7
Receiving objects: 100% (18674/18674), 17.71 MiB | 1.25 MiB/s, done.
8
Resolving deltas: 100% (2656/2656), done.
9
Checking out files: 100% (18446/18446), done.

但是这样clone下来是没有分支信息的,如果需要看不同分支的代码:

1
# git remote set-branches origin PHP-7.3.8 # 如果需要全部的分支可以  git remote set-branches origin '*'
2
# git fetch --depth=1
3
remote: Enumerating objects: 13268, done.
4
remote: Counting objects: 100% (13266/13266), done.
5
remote: Compressing objects: 100% (6744/6744), done.
6
remote: Total 8549 (delta 5695), reused 4475 (delta 1792), pack-reused 0
7
Receiving objects: 100% (8549/8549), 6.49 MiB | 693.00 KiB/s, done.
8
Resolving deltas: 100% (5695/5695), completed with 3878 local objects.
9
From https://github.com/php/php-src
10
 * branch              master     -> FETCH_HEAD
11
 * [new branch]        PHP-7.3.8  -> origin/PHP-7.3.8
12
git remote set-branches origin 'PHP-7.3.8'
13
git fetch --depth=1

如果转换成常规克隆,需要使用--unshallow

1
# git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
2
# git fetch --unshallow
3
remote: Enumerating objects: 800451, done.
4
remote: Counting objects: 100% (800446/800446), done.
5
remote: Compressing objects: 100% (172689/172689), done.
6
remote: Total 790672 (delta 630180), reused 777010 (delta 616884), pack-reused 0
7
Receiving objects: 100% (790672/790672), 334.08 MiB | 3.63 MiB/s, done.
8
Resolving deltas: 100% (630180/630180), completed with 8325 local objects.
9
From https://github.com/php/php-src
10
 * [new branch]        PEAR_1_4DEV                               -> origin/PEAR_1_4DEV
11
 * [new branch]        PECL                                      -> origin/PECL
12
 * [new branch]        PECL_4_3                                  -> origin/PECL_4_3
13
 * [new branch]        PECL_OPENSSL                              -> origin/PECL_OPENSSL
14
 * [new branch]        PHAR_1_2                                  -> origin/PHAR_1_2
15
 * [new branch]        PHP-4.0                                   -> origin/PHP-4.0
16
 * [new branch]        PHP-4.0.5                                 -> origin/PHP-4.0.5
17
 * [new branch]        PHP-4.0.6                                 -> origin/PHP-4.0.6
18
 * [new branch]        PHP-4.0.7                                 -> origin/PHP-4.0.7
19
 * [new branch]        PHP-4.2.0                                 -> origin/PHP-4.2.0
20
 * [new branch]        PHP-4.2.2                                 -> origin/PHP-4.2.2
21
 * [new branch]        PHP-4.3                                   -> origin/PHP-4.3
22
 ...