我的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
其他回答
下面是我为单个子目录稀疏签出用例编写的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
为了澄清这里的一些好答案,许多答案中概述的步骤假设您在某个地方已经有了远程存储库。
给定:现有的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
我错误地认为必须在原始存储库上设置稀疏签出选项,但事实并非如此:在从远程提取之前,您需要在本地定义所需的目录。远程回购不知道或不关心您只想跟踪回购的一部分。
希望这一澄清对其他人有所帮助。
所以我尝试了这一切,但没有任何效果。。。事实证明,在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/
上面有很多好的想法和脚本。我情不自禁地将它们组合成一个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