如何使用相对路径浅克隆本地git存储库? [英] How to shallow clone a local git repository with a relative path?
问题描述
Shallow clones of local directories require file://
as explained at: git clone: warning: --depth is ignored in local clones; use file:// instead
但是如何使用相对路径呢?
But how to use relative paths with that?
例如:如果我在当前目录中有一个仓库myrepo
,那么我会这样做:
E.g.: if I have a repo myrepo
in the current directory, and then I do:
git clone --depth 1 file://mymodule mymodule2
然后失败,并显示以下信息:
then it fails with:
Cloning into 'mymodule2'...
fatal: No path specified. See 'man git-pull' for valid url syntax
如果我尝试:
git clone --depth 1 file://./mymodule mymodule2
它失败并显示:
Cloning into 'mymodule2'...
fatal: '/./mymodule' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
我能找到的唯一解决方法是将其转换为以/
开头的绝对路径:
The only workaround I could find was to convert it to an absolute path starting with /
:
git clone --depth 1 "file://$(pwd)/mymodule" mymodule2
这种行为是否有深远的原因,或者仅仅是越野车?
Is there a profound reason for this behavior, or it just buggy?
也许文件URI根本不支持相对路径:
Maybe file URIs simply don't support relative paths:
- 文件Uri方案和相对文件
- https://superuser.com/questions/210263 /file-to-point-a-relative-file-to-current-directory
- File Uri Scheme and Relative Files
- https://superuser.com/questions/210263/file-to-point-a-relative-file-to-current-directory
git 2.14.1.
git 2.14.1.
推荐答案
浅浅克隆本地存储库既没有必要,也没有好处.只需git clone ./relative/path
,您就可以使用了.
It's neither necessary nor beneficial to shallow clone a local repo. Just git clone ./relative/path
and you are good to go.
我假设您想进行浅拷贝,因为您想节省磁盘空间并节省时间,但是
I assume you want to shallow copy because you want to save the disk space and save time, but
1.它不会在本地克隆中节省磁盘空间.
从Git 2.24.1手册开始:
From Git 2.24.1 manual:
对于本地存储库(Git本身也支持),以下语法可能是 已使用:
For local repositories, also supported by Git natively, the following syntaxes may be used:
•/path/to/repo.git/
• /path/to/repo.git/
•file:///path/to/repo.git/
• file:///path/to/repo.git/
除了前者隐含--local选项外,这两种语法基本相同.
和
-l,--local
-l, --local
当要从中克隆的存储库位于本地计算机上时,此标志将绕过常规的"Git感知"传输机制,并通过在对象和refs目录下制作HEAD以及所有内容的副本来克隆存储库. .git/objects/目录下的文件经过硬链接以节省空间.
When the repository to clone from is on a local machine, this flag bypasses the normal "Git aware" transport mechanism and clones the repository by making a copy of HEAD and everything under objects and refs directories. The files under .git/objects/ directory are hardlinked to save space when possible.
因此,使用file:///path/to/repo.git/
语法的浅表副本比使用/path/to/repo.git/
语法的完整副本要消耗更多的磁盘空间,因为前者具有对象的真实副本,而不是硬链接.
So a shallow copy with file:///path/to/repo.git/
syntax should consume more disk space than a full copy with /path/to/repo.git/
syntax because the former has real copies of objects instead of hard links.
(我相信硬链接是设置--local
时--depth
被忽略的原因.)
(And I believe that hard-linking is the reason why --depth
get ignored when --local
is set.)
2.也不节省时间.
还是Git 2.24.1手册:
Still, Git 2.24.1 manual:
-l,--local
-l, --local
要克隆的存储库位于本地计算机上时,此标志绕过常规的"Git感知"传输机制,并通过复制HEAD以及对象和refs目录下的所有内容来克隆存储库.尽可能将.git/objects/目录下的文件进行链接以节省空间.
When the repository to clone from is on a local machine, this flag bypasses the normal "Git aware" transport mechanism and clones the repository by making a copy of HEAD and everything under objects and refs directories. The files under .git/objects/ directory are hardlinked to save space when possible.
"Git感知"传输机制即使在URI为file://...
的情况下也很耗时,因为它包括对象压缩:
"Git aware" transport mechanism is time-consuming even when the URI is file://...
because it includes object compressing:
$ time git clone --depth 1 --local --shallow-submodules file://$(pwd)/../linux
Cloning into 'linux'...
warning: --local is ignored
remote: Enumerating objects: 65607, done.
remote: Counting objects: 100% (65607/65607), done.
remote: Compressing objects: 100% (61137/61137), done.
remote: Total 65607 (delta 4886), reused 39965 (delta 3556)
Receiving objects: 100% (65607/65607), 176.65 MiB | 10.81 MiB/s, done.
Resolving deltas: 100% (4886/4886), done.
Updating files: 100% (61793/61793), done.
git clone --depth 1 --local --shallow-submodules 73.55s user 4.97s system 245% cpu 31.976 total
(我不知道为什么NVMe SSD上的接收速度为10.81 MiB/s)
(I don't know why the receiving speed is 10.81 MiB/s on my NVMe SSD)
使用--local
标志进行克隆要快得多:
And cloning with --local
flag is much quicker:
$ time git clone ../linux
Cloning into 'linux'...
done.
Updating files: 100% (61793/61793), done.
git clone ../linux 4.16s user 1.71s system 100% cpu 5.857 total
这篇关于如何使用相对路径浅克隆本地git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!