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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

我创建了一个简单的应用程序,支持下载目录、文件和存储库(私有/公共)。

应用程序:https://downdir.vercel.app/

github:https://github.com/renomureza/downdir

其他回答

我在CentOS 7服务器上工作,但我没有root访问权限,也没有git、svn等(也不想!),所以制作了一个python脚本来下载任何github文件夹:https://github.com/andrrrl/github-folder-downloader

用法很简单,只需从github项目中复制相关部分,假设该项目是https://github.com/MaxCDN/php-maxcdn/,如果您需要一个仅包含某些源文件的文件夹,则需要执行以下操作:

$python gdownload.py“/MaxCDN/php MaxCDN/tree/master/src”/my/target/dir/(如果不存在,将创建目标文件夹)

它需要lxml库,可以与easy_install lxml一起安装如果您没有root访问权限(像我一样),可以在$HOME目录中创建一个.pydistutils.py文件,其中包含以下内容:[安装]用户=1easy_install lxml将正常工作(参考:https://stackoverflow.com/a/33464597/591257).

将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" 

试试看。

https://github.com/twfb/git-directory-download

usage: gitd [-h] [-u URL] [-r] [-p] [--proxy PROXY]

optional arguments:
  -h, --help         show this help message and exit
  -u URL, --url URL  github url, split by ",", example: "https://x, http://y"
  -r, --raw          download from raw url
  -p, --parse        download by parsing html
  --proxy PROXY      proxy config, example "socks5://127.0.0.1:7891"

Example:
  1. download by raw url: gitd -u "https://github.com/twfb/git-directory-download"
  2. download by raw url: gitd -r -u "https://github.com/twfb/git-directory-download"
  3. dowmload by parsing: gitd -p -u "https://github.com/twfb/git-directory-download"
  4. download by raw url with proxy: gitd -r -u "https://github.com/twfb/git-directory-download" --proxy "socks5://127.0.0.1:7891"

您可以将ghget与从地址栏复制的任何URL一起使用:

ghget https://github.com/fivethirtyeight/data/tree/master/airline-safety

这是一个独立的可移植shell脚本,不使用SVN(这对我来说在大型回购中不起作用)。它也不使用API,因此不需要令牌,也不受速率限制。

免责声明:我做到了。

如果您需要以编程方式执行,并且不想依赖SVN,则可以使用GitHubAPI递归下载所有内容。

为了获得灵感,以下是我的红宝石要点:https://gist.github.com/cvengros/b2a7e82f66519d423b6f