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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

有一个名为githubdl的Python3pip包可以做到这一点*:

export GIT_TOKEN=1234567890123456789012345678901234567890123
pip install githubdl
githubdl -u http://github.com/foobar/test -d foo

项目页面在此处

*免责声明:这个包裹是我写的。

其他回答

一个简单的答案是从下面的链接中选择第一个乌龟svn。

https://tortoisesvn.net/downloads.html

安装时打开CLI选项,以便可以从命令行界面使用它。

复制github子目录链接。

实例https://github.com/tensorflow/models/tree/master/research/deeplab

用树干替换树/主

https://github.com/tensorflow/models/trunk/research/deeplab

并且做到了

svn检出https://github.com/tensorflow/models/trunk/research/deeplab

文件将被下载到当前目录中的deeplab文件夹中。

如果您想使用Python和SVN下载特定的GitHub目录,请使用以下代码:

import validators
from svn.remote import RemoteClient

def download_folder(url):
    if 'tree/master' in url:
        url = url.replace('tree/master', 'trunk')

    r = RemoteClient(url)
    r.export('output')


if __name__ == '__main__':
    url = input('Enter folder URL: ')
    if not validators.url(url):
        print('Invalid url')
    else:
        download_folder(url)

您可以在本教程中查看有关此代码和其他GitHub搜索和下载提示的更多详细信息:https://python.gotrained.com/search-github-api/

最简单的方法是使用fetcher

首先,使用以下命令安装fetcher:

npm安装-ggithub文件获取器

然后您可以下载带有URL的文件或文件夹:

fetcher--url=resource_url--out=output_directory

例如:

fetcher--url=“https://github.com/Gyumeijie/github-files-fetcher/blob/master/CHANGELOG.md“--out=/tmp

你不能;不同于Subversion,每个子目录都可以单独检出,Git在整个存储库的基础上运行。

对于需要更细粒度访问的项目,可以使用子模块——每个子模块都是一个单独的Git项目,因此可以单独克隆。

可以想象,Git前端(例如GitHub的web界面或gitweb)可以选择为您提供一个界面来提取给定的文件夹,但据我所知,他们中没有一个这样做(尽管他们确实允许您下载单个文件,所以如果文件夹不包含太多文件,这是一个选项)

编辑-GitHub实际上提供了通过SVN的访问,这将允许您执行此操作(根据评论)。看见https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away有关如何执行此操作的最新说明

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