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

/finisht
/static

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

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

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


当前回答

如果您实际上只对目录的最新修订文件感兴趣,Github允许您下载一个不包含历史记录的Zip文件形式的存储库。所以下载速度要快得多。

其他回答

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

对于其他只想从github下载文件/文件夹的用户,只需使用:

svn export <repo>/trunk/<folder>

e.g.

svn export https://github.com/lodash/lodash.com/trunk/docs

(是的,这里是svn。显然在2016年,您仍然需要svn来下载一些github文件)

提供:从GitHub repo下载单个文件夹或目录

重要信息-请确保更新github URL并将/tree/master/替换为'trunk/'。

作为bash脚本:

git-download(){
    folder=${@/tree\/master/trunk}
    folder=${folder/blob\/master/trunk}
    svn export $folder
}

笔记此方法下载文件夹,但不克隆/签出它。您不能将更改推回到存储库。另一方面,与稀疏签出或浅签出相比,这导致下载量较小。

如果您实际上只对目录的最新修订文件感兴趣,Github允许您下载一个不包含历史记录的Zip文件形式的存储库。所以下载速度要快得多。

这将克隆特定文件夹并删除所有与之无关的历史记录。

git clone --single-branch -b {branch} git@github.com:{user}/{repo}.git
git filter-branch --subdirectory-filter {path/to/folder} HEAD
git remote remove origin
git remote add origin git@github.com:{user}/{new-repo}.git
git push -u origin master

这里有很多很棒的回复,但我想补充一点,在Windows Sever 2016上,使用目录名周围的引号对我来说是失败的。这些文件根本没有被下载。

而不是

"mydir/myfolder"

我不得不使用

mydir/myfolder

此外,如果您想简单地下载所有子目录,只需使用

git sparse-checkout set *