我的Git存储库在根目录下有两个子目录:

/finisht
/static

当这是在SVN中时,/finisht在一个地方签出,而/static在其他地方签出了,如下所示:

svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static

有没有办法用Git做到这一点?


当前回答

所以我尝试了这一切,但没有任何效果。。。事实证明,在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 2.19开始,这终于是可能的,从这个答案中可以看出。

考虑对这个答案投赞成票。

注意:在Git2.19中,只实现了客户端支持,服务器端支持仍然缺失,因此它只在克隆本地存储库时有效。还要注意,大型Git宿主(例如GitHub)实际上并不使用Git服务器,而是使用自己的实现,因此即使Git服务器中显示了支持,也不会自动表示它在Git宿主上运行。(OTOH,因为他们不使用Git服务器,所以在Git服务器出现之前,他们可以在自己的实现中更快地实现它。)


不,这在Git中是不可能的。

在Git中实现这样的东西将是一项巨大的努力,这意味着客户端存储库的完整性将无法再得到保证。如果您感兴趣,请在gitmailinglist上搜索有关“稀疏克隆”和“稀疏获取”的讨论。

一般来说,Git社区的共识是,如果您有几个目录总是独立检出,那么这是两个不同的项目,应该存在于两个不同存储库中。您可以使用Git子模块将它们粘在一起。

下面是我为单个子目录稀疏签出用例编写的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

如果您从未计划与从中克隆的存储库交互,则可以执行完整的git克隆并使用

git filter-branch --subdirectory-filter <subdirectory>

这样,至少历史会被保存下来。

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

这看起来简单得多:

git archive --remote=<repo_url> <branch> <path> | tar xvf -