我的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做到这一点?
当前回答
对于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 "$@"
其他回答
上面有很多好的想法和脚本。我情不自禁地将它们组合成一个bash脚本,并提供帮助和错误检查:
#!/bin/bash
function help {
printf "$1
Clones a specific directory from the master branch of a git repository.
Syntax:
$(basename $0) [--delrepo] repoUrl sourceDirectory [targetDirectory]
If targetDirectory is not specified it will be set to sourceDirectory.
Downloads a sourceDirectory from a Git repository into targetdirectory.
If targetDirectory is not specified, a directory named after `basename sourceDirectory`
will be created under the current directory.
If --delrepo is specified then the .git subdirectory in the clone will be removed after cloning.
Example 1:
Clone the tree/master/django/conf/app_template directory from the master branch of
git@github.com:django/django.git into ./app_template:
\$ $(basename $0) git@github.com:django/django.git django/conf/app_template
\$ ls app_template/django/conf/app_template/
__init__.py-tpl admin.py-tpl apps.py-tpl migrations models.py-tpl tests.py-tpl views.py-tpl
Example 2:
Clone the django/conf/app_template directory from the master branch of
https://github.com/django/django/tree/master/django/conf/app_template into ~/test:
\$ $(basename $0) git@github.com:django/django.git django/conf/app_template ~/test
\$ ls test/django/conf/app_template/
__init__.py-tpl admin.py-tpl apps.py-tpl migrations models.py-tpl tests.py-tpl views.py-tpl
"
exit 1
}
if [ -z "$1" ]; then help "Error: repoUrl was not specified.\n"; fi
if [ -z "$2" ]; then help "Error: sourceDirectory was not specified."; fi
if [ "$1" == --delrepo ]; then
DEL_REPO=true
shift
fi
REPO_URL="$1"
SOURCE_DIRECTORY="$2"
if [ "$3" ]; then
TARGET_DIRECTORY="$3"
else
TARGET_DIRECTORY="$(basename $2)"
fi
echo "Cloning into $TARGET_DIRECTORY"
mkdir -p "$TARGET_DIRECTORY"
cd "$TARGET_DIRECTORY"
git init
git remote add origin -f "$REPO_URL"
git config core.sparseCheckout true
echo "$SOURCE_DIRECTORY" > .git/info/sparse-checkout
git pull --depth=1 origin master
if [ "$DEL_REPO" ]; then rm -rf .git; fi
所以我尝试了这一切,但没有任何效果。。。事实证明,在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 repos时使用svn:/我一直使用这个;
function git-scp() (
URL="$1" && shift 1
svn export ${URL/blob\/master/trunk}
)
这允许您无需修改即可从github url中复制。用法
--- /tmp » git-scp https://github.com/dgraph-io/dgraph/blob/master/contrib/config/kubernetes/helm 1 ↵
A helm
A helm/Chart.yaml
A helm/README.md
A helm/values.yaml
Exported revision 6367.
--- /tmp » ls | grep helm
Permissions Size User Date Modified Name
drwxr-xr-x - anthony 2020-01-07 15:53 helm/
@Chronial的anwser不再适用于最近的版本,但它是一个有用的Anwsr,因为它提出了一个脚本。
考虑到我收集的信息以及我只想签出分支的子目录这一事实,我创建了以下shell函数。它只获取分支中提供的目录的最新版本的浅拷贝。
function git_sparse_clone_branch() (
rurl="$1" localdir="$2" branch="$3" && shift 3
git clone "$rurl" --branch "$branch" --no-checkout "$localdir" --depth 1 # limit history
cd "$localdir"
# git sparse-checkout init --cone # fetch only root file
# Loops over remaining args
for i; do
git sparse-checkout set "$i"
done
git checkout "$branch"
)
因此,示例使用:
git_sparse_clone_branch git@github.com:user/repo.git localpath branch-to-clone path1_to_fetch path2_to_fetch
在我的案例中,克隆“仅”为23MB,而完整克隆为385MB。
使用git版本2.36.1进行测试。
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