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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

无论谁在处理特定的文件夹,他都需要克隆该特定的文件夹本身,为此,请使用稀疏签出执行以下步骤。

创建目录。初始化Git存储库。(git初始化)启用稀疏检出。(git-config-core.sparsecheckout true)告诉Git你想要哪些目录(echo 2015/brand/May(参考你想要处理的文件夹)>>.Git/info/spease checkout)添加remote(gitremoteadd-f源https://jafartke.com/mkt-imdev/DVM.git)获取文件(git pull-origin master)

其他回答

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

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

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

}

然后用

source ~/.bashrc 

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

试试看。

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"

在尝试了所有答案后,对我来说最好的解决方案是:

GitHub基于vscode的编辑器。

赞成的意见:

不需要任何额外的工具,如svn或API令牌。内容大小无限制另存为目录或文件,而不是存档。

说明书

转到任何回购。(例如。https://github.com/RespiraWorks/Ventilator/tree/master/software)按或者在URL中将.com替换为.dev,以在GitHub的内部编辑器中打开repo在资源管理器窗格(左侧或按Ctrl+Shift+E)中,右键单击所需的文件/文件夹并选择下载。在“选择文件夹”对话框中,选择磁盘上希望所选文件/文件夹存在的目录。

Note

我尝试了其他解决方案,如接受的答案,

不要只为此安装和学习svn。其他工具,如Download Directory、Refined GitHub、GitZip和DownGit,要么需要API令牌,要么无法下载大型目录。

其他选项

带有远程存储库扩展名的VSCode,用于打开存储库并下载文件/文件夹。

无论谁在处理特定的文件夹,他都需要克隆该特定的文件夹本身,为此,请使用稀疏签出执行以下步骤。

创建目录。初始化Git存储库。(git初始化)启用稀疏检出。(git-config-core.sparsecheckout true)告诉Git你想要哪些目录(echo 2015/brand/May(参考你想要处理的文件夹)>>.Git/info/spease checkout)添加remote(gitremoteadd-f源https://jafartke.com/mkt-imdev/DVM.git)获取文件(git pull-origin master)