gpt4 book ai didi

git - 如何浅克隆具有相对路径的本地 git 存储库?

转载 作者:太空狗 更新时间:2023-10-29 13:16:06 24 4
gpt4 key购买 nike

本地目录的浅克隆需要 file://,如以下解释:git clone: warning: --depth is ignored in local clones; use file:// instead

但是如何使用相对路径呢?

例如:如果我在当前目录中有一个 repo myrepo,然后我会这样做:

git clone --depth 1 file://mymodule mymodule2

然后它失败了:

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.

我能找到的唯一解决方法是将其转换为以 / 开头的绝对路径:

git clone --depth 1 "file://$(pwd)/mymodule" mymodule2

这种行为是否有深刻的原因,或者它只是错误?

也许文件 URI 根本不支持相对路径:

git 2.14.1.

最佳答案

浅克隆本地存储库既没有必要也没有好处。只需 git clone ./relative/path 就可以了。

我假设你想要浅拷贝是因为你想节省磁盘空间和时间,但是

<强>1。它不会在本地克隆中节省磁盘空间。

来自 Git 2.24.1 手册:

For local repositories, also supported by Git natively, the following syntaxes may be used:

• /path/to/repo.git/

• file:///path/to/repo.git/

These two syntaxes are mostly equivalent, except the former implies --local option.

-l, --local

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 的完整副本消耗更多的磁盘空间/ 语法,因为前者具有对象的真实副本而不是硬链接(hard link)。

(我相信硬链接(hard link)是设置 --local--depth 被忽略的原因。)

<强>2。它也不会节省时间。

仍然是 Git 2.24.1 手册:

-l, --local

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.

即使 URI 是 file://...,“Git aware”传输机制也很耗时,因为它包括对象压缩:

$ 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.81MiB/s)

并且使用 --local 标志进行克隆要快得多:

$ 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 - 如何浅克隆具有相对路径的本地 git 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47307578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com