我的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做到这一点?
当前回答
我为执行“稀疏签出”编写了.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
其他回答
如果要克隆gitclone--不签出<REPOSTORY_URL>cd<REPOSTORY_NAME>现在,设置您希望拉入工作目录的特定文件/目录:git稀疏检出集<PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>然后,您应该将工作目录重新设置为您希望提取的提交。例如,我们将其重置为默认的origin/master的HEAD提交。git reset—硬头如果您想gitinit然后远程添加初始化git远程添加原点<REPOSTORY_URL>现在,设置您希望拉入工作目录的特定文件/目录:git稀疏检出集<PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>最后一次提交:git拉动原点主机
注:如果您想将另一个目录/文件添加到工作目录,可以这样做:git稀疏签出添加<PATH_TO_ANOTHER_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>如果要将所有存储库添加到工作目录,请执行以下操作:git稀疏签出添加*如果要清空工作目录,请执行以下操作:git稀疏签出集为空
如果需要,可以通过运行以下命令来查看已指定的跟踪文件的状态:
git status
如果要退出稀疏模式并克隆所有存储库,应运行:
git sparse-checkout set *
git sparse-checkout set init
git sparse-checkout set disable
使用Linux?并且只想要容易访问和清理工作树?而不必麻烦机器上的其他代码。尝试符号链接!
git clone https://github.com:{user}/{repo}.git ~/my-project
ln -s ~/my-project/my-subfolder ~/Desktop/my-subfolder
测验
cd ~/Desktop/my-subfolder
git status
对于其他只想从github下载文件/文件夹的用户,只需使用:
svn export <repo>/trunk/<folder>
e.g.
svn export https://github.com/lodash/lodash.com/trunk/docs
(是的,这里是svn。显然在2016年,您仍然需要svn来下载一些github文件)
提供:从GitHub repo下载单个文件夹或目录
重要信息-请确保更新github URL并将/tree/master/替换为'trunk/'。
作为bash脚本:
git-download(){
folder=${@/tree\/master/trunk}
folder=${folder/blob\/master/trunk}
svn export $folder
}
笔记此方法下载文件夹,但不克隆/签出它。您不能将更改推回到存储库。另一方面,与稀疏签出或浅签出相比,这导致下载量较小。
这将克隆特定文件夹并删除所有与之无关的历史记录。
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
所以我尝试了这一切,但没有任何效果。。。事实证明,在Git的2.24版本(在回答这个问题时随cpanel提供的版本)上,您不需要这样做
echo "wpm/*" >> .git/info/sparse-checkout
你只需要文件夹名
wpm/*
总之,你要这样做
git config core.sparsecheckout true
然后编辑.git/info/spease签出并在末尾添加带有/*的文件夹名称(每行一个)以获取子文件夹和文件
wpm/*
保存并运行checkout命令
git checkout master
结果是我的存储库中的预期文件夹,没有其他内容如果这对你有用,请投票