我的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 archive --remote=<repo_url> <branch> <path> | tar xvf -
其他回答
这看起来简单得多:
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 "<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
对于macOS用户
对于zsh用户(特别是macOS用户)使用ssh克隆Repos,我只需要根据@Ciro Santilli的回答创建一个zsh命令:
要求:git的版本很重要。由于--sparse选项,它在2.25.1上不起作用。尝试将git升级到最新版本。(例如测试2.36.1)
示例用法:
git clone git@github.com:google-research/google-research.git etcmodel
代码:
function gitclone {
readonly repo_root=${1?Usage: gitclone repo.git sub_dir}
readonly repo_sub=${2?Usage: gitclone repo.git sub_dir}
echo "-- Cloning $repo_root/$repo_sub"
git clone \
--depth 1 \
--filter=tree:0 \
--sparse \
$repo_root \
;
repo_folder=${repo_root#*/}
repo_folder=${repo_folder%.*}
cd $repo_folder
git sparse-checkout set $repo_sub
cd -
}
gitclone "$@"
它对我有用-(git版本2.35.1)
git init
git remote add origin <YourRepoUrl>
git config core.sparseCheckout true
git sparse-checkout set <YourSubfolderName>
git pull origin <YourBranchName>
我写了一个从GitHub下载子目录的脚本。
用法:
python get_git_sub_dir.py path/to/sub/dir <RECURSIVE>