我的Git存储库在根目录下有两个子目录:
/finisht
/static
当这是在SVN中时,/finisht在一个地方签出,而/static在其他地方签出了,如下所示:
svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static
有没有办法用Git做到这一点?
我的Git存储库在根目录下有两个子目录:
/finisht
/static
当这是在SVN中时,/finisht在一个地方签出,而/static在其他地方签出了,如下所示:
svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static
有没有办法用Git做到这一点?
当前回答
这里有很多很棒的回复,但我想补充一点,在Windows Sever 2016上,使用目录名周围的引号对我来说是失败的。这些文件根本没有被下载。
而不是
"mydir/myfolder"
我不得不使用
mydir/myfolder
此外,如果您想简单地下载所有子目录,只需使用
git sparse-checkout set *
其他回答
Git1.7.0有“稀疏签出”。看见git-config手册页中的“core.sparceCheckout”,git read树手册页中的“稀疏签出”,以及git更新索引手册页中的“跳过工作树位”。
界面不如SVN方便(例如,在初始克隆时无法进行稀疏签出),但可以构建更简单界面的基本功能现在可用。
(扩展此答案)
克隆特定标记中的子目录
如果要克隆特定标记的特定子目录,可以执行以下步骤。
我在cxf-3.5.4标签中克隆了cxf github repo的发行版/src/main/release/samples/子目录。
注意:如果您尝试仅克隆上述回购,您将看到它非常大。下面的命令仅克隆所需的内容。
git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
git fetch --depth 1 origin cxf-3.5.4
# This is the hash on which the tag points, however using the tag does not work.
git switch --detach 3ef4fde
克隆特定分支中的子目录
我在2.6.x-fixes分支中克隆了cxf github repo的发行版/src/main/release/samples/子目录。
git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf --branch 2.6.x-fixes
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
它对我有用-(git版本2.35.1)
git init
git remote add origin <YourRepoUrl>
git config core.sparseCheckout true
git sparse-checkout set <YourSubfolderName>
git pull origin <YourBranchName>
您仍然可以使用svn:
svn export https://admin@domain.example/home/admin/repos/finisht/static static --force
到“gitclone”子目录,然后到“gitpull”子目录。
(并非旨在提交和推送。)
2022答案
我不知道为什么这个问题有这么多复杂的答案。通过将repo稀疏克隆到所需的文件夹,可以轻松地完成此操作。
导航到要克隆子目录的文件夹。打开cmd并运行以下命令。git clone--filter=blob:none--稀疏%您的git repo url%git稀疏签出添加要克隆的%子目录%cd%您的子目录%
瞧!现在,您只克隆了所需的子目录!
解释-这些命令到底在做什么?
git clone--filter=blob:none--稀疏%您的git repo url%
在上述命令中,
--filter=blob:none=>告诉git您只想克隆元数据文件。通过这种方式,git从远程收集基本的分支详细信息和其他元数据,这将确保您将来从源站顺利签出。--稀疏=>告诉git这是一个稀疏克隆。在这种情况下,Git将只签出根目录。
现在,git被告知元数据,并准备签出您要使用的任何子目录/文件。
git sparse-checkout add gui-workspace ==> Checkout folder
git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file
稀疏克隆在具有多个子目录的大型存储库中特别有用,而您并不总是在处理所有子目录。在大型存储库上执行稀疏克隆时,可以节省大量时间和带宽。
此外,现在,在这个部分克隆的repo中,您可以像往常一样继续结账和工作。所有这些命令都能完美工作。
git switch -c %new-branch-name% origin/%parent-branch-name% (or) git checkout -b %new-branch-name% origin/%parent-branch-name%
git commit -m "Initial changes in sparse clone branch"
git push origin %new-branch-name%