我的Git存储库在根目录下有两个子目录:

/finisht
/static

当这是在SVN中时,/finisht在一个地方签出,而/static在其他地方签出了,如下所示:

svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static

有没有办法用Git做到这一点?


当前回答

(扩展此答案)

克隆特定标记中的子目录

如果要克隆特定标记的特定子目录,可以执行以下步骤。

我在cxf-3.5.4标签中克隆了cxf github repo的发行版/src/main/release/samples/子目录。

注意:如果您尝试仅克隆上述回购,您将看到它非常大。下面的命令仅克隆所需的内容。

git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
git fetch --depth 1 origin cxf-3.5.4
# This is the hash on which the tag points, however using the tag does not work.
git switch --detach 3ef4fde

克隆特定分支中的子目录

我在2.6.x-fixes分支中克隆了cxf github repo的发行版/src/main/release/samples/子目录。

git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf --branch 2.6.x-fixes
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/

其他回答

使用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

@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进行测试。

如果要克隆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

2022答案

我不知道为什么这个问题有这么多复杂的答案。通过将repo稀疏克隆到所需的文件夹,可以轻松地完成此操作。

导航到要克隆子目录的文件夹。打开cmd并运行以下命令。git clone--filter=blob:none--稀疏%您的git repo url%git稀疏签出添加要克隆的%子目录%cd%您的子目录%

瞧!现在,您只克隆了所需的子目录!

解释-这些命令到底在做什么?

git clone--filter=blob:none--稀疏%您的git repo url%

在上述命令中,

--filter=blob:none=>告诉git您只想克隆元数据文件。通过这种方式,git从远程收集基本的分支详细信息和其他元数据,这将确保您将来从源站顺利签出。--稀疏=>告诉git这是一个稀疏克隆。在这种情况下,Git将只签出根目录。

现在,git被告知元数据,并准备签出您要使用的任何子目录/文件。

git sparse-checkout add gui-workspace ==> Checkout folder

git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file

稀疏克隆在具有多个子目录的大型存储库中特别有用,而您并不总是在处理所有子目录。在大型存储库上执行稀疏克隆时,可以节省大量时间和带宽。

此外,现在,在这个部分克隆的repo中,您可以像往常一样继续结账和工作。所有这些命令都能完美工作。

git switch -c  %new-branch-name% origin/%parent-branch-name% (or) git checkout -b %new-branch-name% origin/%parent-branch-name% 
git commit -m "Initial changes in sparse clone branch"
git push origin %new-branch-name%

我不知道是否有人成功拉取了特定目录,这是我的经验:gitclone--filter=blob:none--singlebranch<repo>,下载对象时立即取消,输入repo,然后gitcheckoutorigin/master<dir>,忽略错误(sha1),输入dir,对每个子目录重复签出(使用新的dir)。我设法以这种方式快速获取源文件