Download a single folder or directory from a GitHu

2019-01-01 11:16发布

How can I download only a specific folder or directory from a remote Git repo hosted on GitHub?

Say the example GitHub repo lives here:

git@github.com:foobar/Test.git

Its directory structure:

Test/
    foo/ 
       a.py
       b.py
    bar/
       c.py
       d.py

I want to download only the foo folder and not clone the whole Test project.

标签: git github
26条回答
裙下三千臣
2楼-- · 2019-01-01 11:56

Update Sep. 2016: there are a few tools created by the community that can do this for you:


Git doesn't support this, but Github does via SVN. If you checkout your code with subversion, Github will essentially convert the repo from git to subversion on the backend, then serve up the requested directory.

Here's how you can use this feature to download a specific folder. I'll use the popular javascript library lodash as an example.

  1. Get the repo URL. First, copy the URL of the Github repo to your clipboard. github repo URL example

  2. Modify the URL for subversion. I want to download the folder at /docs from the master branch, so I will append trunk/docs. Full URL is now https://github.com/lodash/lodash/trunk/docs. See my note below for a more in-depth explanation of why we must use this URL format.

  3. Download the folder. Go to the command line and grab the folder with SVN. svn checkout https://github.com/lodash/lodash/trunk/docs

You might not see any activity immediately because Github takes up to 30 seconds to convert larger repositories, so be patient.

Full URL format explanation:

  • If you're interested in master branch, use trunk instead. So the full path is trunk/foldername
  • If you're interested in foo branch, use branches/foo instead. The full path looks like branches/foo/foldername
  • Protip: You can use svn ls to see available tags and branches before downloading if you wish

That's all! Github supports more subversion features as well, including support for committing and pushing changes.

查看更多
余生无你
3楼-- · 2019-01-01 11:56

I wrote a tool with Node.js just for this. Check it out Download Repo Dir

install with npm i -g dl-repo-dir and command repo will be available globally.

download and rename a directory in a repository

repo download aztack/download-repo-dir lib src/lib/new-name

download a repository

repo download aztack/download-repo-dir '' src/lib/download-repo-dir

download from a private gitlab repository with given tag

export GITLAB_API_PRIVATE_TOKEN=YOUR_TOKEN_HERE

repo download gitlab:mygitlab.com:topgroup/subgroup/repo#v1.0.0 dir src/lib/new-name

and there will be a repo.json file to save all the information.

In a new project, you can initialize the project with exists repo.json using repo init command.

查看更多
君临天下
4楼-- · 2019-01-01 11:57

I use linux so , put this in ~/.bashrc , called even :D $HOME/.bashrc

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

}

then refresh the shell with

source ~/.bashrc 

then use it with git-downloadfolder blablabla :D

查看更多
梦该遗忘
5楼-- · 2019-01-01 11:58

Two options for this feature:

Option 1: Browser Extensions

Chrome Extension, Firefox Addon

Usage:

  1. In any GitHub repos page.
  2. Just double click on the items you need.
  3. Click download button at bottom-right.
  4. See the progress dashboard and wait for browser trigger download.
  5. Get the ZIP file.

Get Token:

  1. Click GitZip Extension icon on your browser.
  2. Click "Normal" or "Private" link besides "Get Token".
  3. Authorize GitZip permission on Github auth page.
  4. Back to repo page of the beginning.
  5. Continue to use.

Option 2: Github gh-page

http://kinolien.github.io/gitzip by using GitHub API, and JSZip, FileSaver.js libraries.

Step1: Input github url to the field at the top-right.
Step2: Press enter or click download for download zip directly or click search for view the list of sub-folders and files.
Step3: Click "Download Zip File" or "Get File" button to get files.

In most cases, it works fine, except that the folder contains more than 1,000 files, because of the Github Trees API limitation. (refers to Github API#Contents)

And it also can support private/public repos and upgrade the rate limit, if you have GitHub account and use "get token" link in this site.

查看更多
冷夜・残月
6楼-- · 2019-01-01 11:59

I work with CentOS 7 servers on which I don't have root access, nor git, svn, etc (nor want to!) so made a python script to download any github folder: https://github.com/andrrrl/github-folder-downloader

Usage is simple, just copy the relevant part from a github project, let's say the project is https://github.com/MaxCDN/php-maxcdn/, and you want a folder where some source files are only, then you need to do something like:

$ python gdownload.py "/MaxCDN/php-maxcdn/tree/master/src" /my/target/dir/
(will create target folder if doesn't exist)

It requires lxml library, can be installed with easy_install lxml
If you don't have root access (like me) you can create a .pydistutils.py file into your $HOME dir with these contents: [install] user=1 And easy_install lxml will just work (ref: https://stackoverflow.com/a/33464597/591257).

查看更多
其实,你不懂
7楼-- · 2019-01-01 11:59

If you are comfortable with unix commands, you don't need special dependencies or web apps for this. You can download the repo as a tarball and untar only what you need.

Example (woff2 files from a subdirectory in fontawesome):

curl -L https://api.github.com/repos/FortAwesome/Font-Awesome/tarball | tar xz --wildcards "*/web-fonts-with-css/webfonts/*.woff2" --strip-components=3
  • More about the link format: https://developer.github.com/v3/repos/contents/#get-archive-link (including how to get a zip file or specific branches/refs)
  • Keep the initial part of the path (*/) to match any directory. Github creates a wrapper directory with the commit ref in the name, so it can't be known.
  • You probably want --strip-components to be the same as the amount of slashes (/) in the path (previous argument).

This will download the whole tarball. Use the SVN method mentioned in the other answers if this has to be avoided or if you want to be nice to the GitHub servers.

查看更多
登录 后发表回答