我的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存储库,例如。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
我错误地认为必须在原始存储库上设置稀疏签出选项,但事实并非如此:在从远程提取之前,您需要在本地定义所需的目录。远程回购不知道或不关心您只想跟踪回购的一部分。
希望这一澄清对其他人有所帮助。
其他回答
如果您实际上只对目录的最新修订文件感兴趣,Github允许您下载一个不包含历史记录的Zip文件形式的存储库。所以下载速度要快得多。
这将克隆特定文件夹并删除所有与之无关的历史记录。
git clone --single-branch -b {branch} git@github.com:{user}/{repo}.git
git filter-branch --subdirectory-filter {path/to/folder} HEAD
git remote remove origin
git remote add origin git@github.com:{user}/{new-repo}.git
git push -u origin master
我为执行“稀疏签出”编写了.gitconfig[别名]。检查一下(没有双关语):
在Windows上运行cmd.exe
git config --global alias.sparse-checkout "!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p \"$L/.git/info\" && cd \"$L\" && git init --template= && git remote add origin \"$1\" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo \"$2\" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f"
否则:
git config --global alias.sparse-checkout '!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p "$L/.git/info" && cd "$L" && git init --template= && git remote add origin "$1" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo "$2" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f'
用法:
# Makes a directory ForStackExchange with Plug checked out
git sparse-checkout https://github.com/YenForYang/ForStackExchange Plug
# To do more than 1 directory, you have to specify the local directory:
git sparse-checkout https://github.com/YenForYang/ForStackExchange ForStackExchange Plug Folder
为了方便和存储,git-config命令被“缩小”了,但这里扩展了别名:
# Note the --template= is for disabling templates.
# Feel free to remove it if you don't have issues with them (like I did)
# `mkdir` makes the .git/info directory ahead of time, as I've found it missing sometimes for some reason
f(){
[ "$#" -eq 2 ] && L="${1##*/}" L=${L%.git} || L=$2;
mkdir -p "$L/.git/info"
&& cd "$L"
&& git init --template=
&& git remote add origin "$1"
&& git config core.sparseCheckout 1;
[ "$#" -eq 2 ]
&& echo "$2" >> .git/info/sparse-checkout
|| {
shift 2;
for i; do
echo $i >> .git/info/sparse-checkout;
done
};
git pull --depth 1 origin master;
};
f
您可以结合稀疏检出和浅层克隆功能。浅层克隆会切断历史记录,稀疏签出只会提取与模式匹配的文件。
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存档中是不可能的。
Git1.7.0有“稀疏签出”。看见git-config手册页中的“core.sparceCheckout”,git read树手册页中的“稀疏签出”,以及git更新索引手册页中的“跳过工作树位”。
界面不如SVN方便(例如,在初始克隆时无法进行稀疏签出),但可以构建更简单界面的基本功能现在可用。