如何从GitHub上托管的远程Git repo中仅下载特定文件夹或目录?

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

Test/
├── foo/ 
│   ├── a.py
│   └── b.py   
└── bar/
    ├── c.py
    └── d.py

我只想下载foo文件夹,而不是克隆整个测试项目。


当前回答

我使用linux,所以把它放在~/.bashrc中,甚至称为:D$HOME/.bashrc

git-dowloadfolder(){
a="$1"
svn checkout ${a/tree\/master/trunk}

}

然后用

source ~/.bashrc 

然后将其与git downloadfolder blablabla:D一起使用

其他回答

通过在url中将github替换为githubbox,打开repo到codesandbox,然后在codesandbox上转到文件菜单并将其导出为zip。

对于以下回购:https://github.com/geist-org/react/tree/master/examples/custom-themes

输入以下url:https://githubbox.com/geist-org/react/tree/master/examples/custom-themes

在codesandbox中,转到文件菜单并将其导出为Zip。

我使用linux,所以把它放在~/.bashrc中,甚至称为:D$HOME/.bashrc

git-dowloadfolder(){
a="$1"
svn checkout ${a/tree\/master/trunk}

}

然后用

source ~/.bashrc 

然后将其与git downloadfolder blablabla:D一起使用

将git存储库文件夹下载到当前目录并删除git文件。

#!/bin/sh    

function download_git_folder() {
  repo_url=$1
  branch=$2
  repo_subfolder_path=$3
  
  repo_folder=$(basename $repo_url)
  git init
  git remote add -f origin ${repo_url}
  git config core.sparseCheckout true
  echo "${repo_subfolder_path}" >> .git/info/sparse-checkout
  git pull origin ${branch}
  mv "${repo_subfolder_path}"/* ./

  readarray -td/ root_subfolder <<<"${repo_subfolder_path}"; declare -p root_subfolder;
  rm -rf ./.git ${root_subfolder[0]}
}

用法

download_git_folder "git@github.com:foobar/Test.git" "master" "Test/bar" 

这是SVN优于Git的少数几个地方之一。

最终,我们倾向于三种选择:

使用wget从GitHub获取数据(使用原始文件视图)。让上游项目将所需的数据子集发布为构建工件。放弃并使用全额结账。它在第一个版本中大受欢迎,但除非您获得大量流量,否则在接下来的版本中不会太麻烦。

可以按以下方式使用git-svn。

首先,用主干替换树/主节点然后,通过sudoaptinstallgitsvn安装gitsvn

git svn clone https://github.com/lodash/lodash/trunk/test

这样,您就不必经历设置svn的痛苦,特别是针对Windows用户。