我的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做到这一点?
当前回答
这看起来简单得多:
git archive --remote=<repo_url> <branch> <path> | tar xvf -
其他回答
(扩展此答案)
克隆特定标记中的子目录
如果要克隆特定标记的特定子目录,可以执行以下步骤。
我在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.24版本(在回答这个问题时随cpanel提供的版本)上,您不需要这样做
echo "wpm/*" >> .git/info/sparse-checkout
你只需要文件夹名
wpm/*
总之,你要这样做
git config core.sparsecheckout true
然后编辑.git/info/spease签出并在末尾添加带有/*的文件夹名称(每行一个)以获取子文件夹和文件
wpm/*
保存并运行checkout命令
git checkout master
结果是我的存储库中的预期文件夹,没有其他内容如果这对你有用,请投票
仅使用Git是不可能克隆子目录的,但以下是一些解决方法。
过滤器分支
您可能希望重写存储库,使其看起来像trunk/public_html/是它的项目根,并放弃所有其他历史记录(使用过滤器分支),尝试已经签出的分支:
git filter-branch --subdirectory-filter trunk/public_html -- --all
注意:--将筛选器分支选项与修订选项分开,--all用于重写所有分支和标记。将保留所有信息,包括原始提交时间或合并信息。此命令接受refs/replace/namespace中的.git/info/places文件和ref,因此如果定义了任何移植或替换ref,运行此命令将使其永久化。
警告重写的历史将对所有对象具有不同的对象名称,并且不会与原始分支汇合。您将无法在原始分支的顶部轻松推送和分发重写的分支。如果您不知道完整的含义,请不要使用此命令,如果一次简单的提交就足以解决您的问题,请避免使用它。
稀疏校验
以下是稀疏签出方法的简单步骤,它将稀疏地填充工作目录,因此您可以告诉Git工作目录中的哪个文件夹或文件值得签出。
照常克隆存储库(--不选择签出):gitclone--不签出git@foo/巴.吉特cd条如果已经克隆了存储库,则可以跳过此步骤。提示:对于大型回购,请考虑浅层克隆(--depth 1)以仅签出最新版本或/和--仅签出单个分支。启用spareCheckout选项:git-config-core.sparseCheckout true指定用于稀疏签出的文件夹(末尾没有空格):echo“trunk/public_html/*”>.git/info/s稀疏签出或edit.git/info/s稀疏签出。签出分支(例如主分支):切换到主分支
现在,您应该在当前目录中选择了文件夹。
如果有太多级别的目录或过滤分支,可以考虑使用符号链接。
下面是我为单个子目录稀疏签出用例编写的shell脚本
co子目录.sh
localRepo=$1
remoteRepo=$2
subDir=$3
# Create local repository for subdirectory checkout, make it hidden to avoid having to drill down to the subfolder
mkdir ./.$localRepo
cd ./.$localRepo
git init
git remote add -f origin $remoteRepo
git config core.sparseCheckout true
# Add the subdirectory of interest to the sparse checkout.
echo $subDir >> .git/info/sparse-checkout
git pull origin master
# Create convenience symlink to the subdirectory of interest
cd ..
ln -s ./.$localRepo/$subDir $localRepo
这里有很多很棒的回复,但我想补充一点,在Windows Sever 2016上,使用目录名周围的引号对我来说是失败的。这些文件根本没有被下载。
而不是
"mydir/myfolder"
我不得不使用
mydir/myfolder
此外,如果您想简单地下载所有子目录,只需使用
git sparse-checkout set *