我的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做到这一点?
当前回答
如果要克隆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
其他回答
我为执行“稀疏签出”编写了.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
下面是我为单个子目录稀疏签出用例编写的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
使用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
Git1.7.0有“稀疏签出”。看见git-config手册页中的“core.sparceCheckout”,git read树手册页中的“稀疏签出”,以及git更新索引手册页中的“跳过工作树位”。
界面不如SVN方便(例如,在初始克隆时无法进行稀疏签出),但可以构建更简单界面的基本功能现在可用。
所以我尝试了这一切,但没有任何效果。。。事实证明,在Git的2.24版本(在回答这个问题时随cpanel提供的版本)上,您不需要这样做
echo "wpm/*" >> .git/info/sparse-checkout
你只需要文件夹名
wpm/*
总之,你要这样做
git config core.sparsecheckout true
然后编辑.git/info/spease签出并在末尾添加带有/*的文件夹名称(每行一个)以获取子文件夹和文件
wpm/*
保存并运行checkout命令
git checkout master
结果是我的存储库中的预期文件夹,没有其他内容如果这对你有用,请投票