我的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 -

其他回答

您可以结合稀疏检出和浅层克隆功能。浅层克隆会切断历史记录,稀疏签出只会提取与模式匹配的文件。

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master

您需要最低1.9吉才能工作。仅使用2.2.0和2.2.2自行测试。

这样,您仍然可以进行推送,这在git存档中是不可能的。

degit制作git存储库的副本。当您运行degit时一些用户/一些repo,它将在https://github.com/some-user/some-repo并下载相关的tar文件到~/.degit/some user/some repo/commithash.tar.gz(如果没有)已在本地存在。(这比使用git clone快得多,因为你没有下载整个git历史记录。)

degit <https://github.com/user/repo/subdirectory> <output folder>

了解更多信息https://www.npmjs.com/package/degit

您仍然可以使用svn:

svn export https://admin@domain.example/home/admin/repos/finisht/static static --force

到“gitclone”子目录,然后到“gitpull”子目录。

(并非旨在提交和推送。)

为了澄清这里的一些好答案,许多答案中概述的步骤假设您在某个地方已经有了远程存储库。

给定:现有的git存储库,例如。git@github.com:some user/fullrepo.git,其中包含一个或多个您希望独立于repo其余部分拉动的目录,例如名为app1和app2的目录

假设您有一个如上所述的git存储库。。。

然后:您可以运行以下步骤,从较大的存储库中仅提取特定目录:

mkdir app1
cd app1
git init
git remote add origin git@github.com:some-user/full-repo.git
git config core.sparsecheckout true
echo "app1/" >> .git/info/sparse-checkout
git pull origin master

我错误地认为必须在原始存储库上设置稀疏签出选项,但事实并非如此:在从远程提取之前,您需要在本地定义所需的目录。远程回购不知道或不关心您只想跟踪回购的一部分。

希望这一澄清对其他人有所帮助。

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "<path you want to clone>/*" >> .git/info/sparse-checkout
git pull --depth=1 origin <branch you want to fetch>

仅从此回购中克隆Jetsurvey文件夹的示例

git init MyFolder
cd MyFolder 
git remote add origin git@github.com:android/compose-samples.git
git config core.sparsecheckout true
echo "Jetsurvey/*" >> .git/info/sparse-checkout
git pull --depth=1 origin main