Download a single folder or directory from a GitHu

2019-01-01 11:48发布

问题:

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.

回答1:

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

  • GitZip (Credits to Kino - upvote his answer right here!)

  • DownGit (Credits to Minhas Kamal - upvote his answer right here!)


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

  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.



回答2:

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.



回答3:

Go to DownGit > Enter Your URL > Download!

Now, you can DIRECTLY DOWNLOAD or create DOWNLOAD LINK for any GitHub public directory or file (specially large file) from DownGit! Here is a simple demonstration-


\"DownGit\"


You may also configure the downloaded file\'s property- detailed usage.



回答4:

If you have svn, you can use svn export to do this:

svn export https://github.com/foobar/Test.git/trunk/foo

Notice the URL format:

  • The base URL is https://github.com/
  • /trunk appended at the end

Before you run svn export, it\'s good to first verify the content of the directory with:

svn ls https://github.com/foobar/Test.git/trunk/foo


回答5:

For a Generic git Repo:

If you want to download files, not clone the repository with history, you can do this with git-archive.

git-archive makes a compressed zip or tar archive of a git repository. Some things that make it special:

  1. You can choose which files or directories in the git repository to archive.
  2. It doesn\'t archive the .git/ folder, or any untracked files in the repository it\'s run on.
  3. You can archive a specific branch, tag, or commit. Projects managed with git often use this to generate archives of versions of the project (beta, release, 2.0, etc.) for users to download.

An example of creating an archive of the docs/usage directory from a remote repo you\'re connected to with ssh:

# in terminal
$ git archive --format tar --remote ssh://server.org/path/to/git HEAD docs/usage > /tmp/usage_docs.tgz

More information in this blog post and the git documentation.

Note on GitHub Repos:

GitHub doesn\'t allow git-archive access. ☹️



回答6:

I\'ve created an open source project, called GitHubFolderDownloader. It lets you to download a single folder of a repository without cloning or downloading the whole repository.



回答7:

Nothing wrong with other answers but I just thought I\'d share step-by-step instructions for those wandering through this process for the first time.

How to download a single folder from a github repository (Mac OS X):

~ To open Terminal just click spotlight and type terminal then hit enter

  1. On a Mac you likely already have SVN (to test just open terminal and type \"svn\" or \"which svn\" ~ without the quote marks)
  2. On Github: Locate the Github path to your git folder (not the repo) by clicking the specific folder name within a repo
  3. Copy the path from the address bar of the browser
  4. Open Terminal and type: svn export
  5. Next paste in the address (eg.): https://github.com/mingsai/Sample-Code/tree/master/HeadsUpUI
  6. Replace the words: tree/master
  7. with the word: trunk
  8. Type in the destination folder for the files (in this example, I store the target folder inside of the Downloads folder for the current user)
  9. Here space is just the spacebar not the word (space) ~/Downloads/HeadsUpUI
  10. The final terminal command shows the full command to download the folder (compare the address to step 5) svn export https://github.com/mingsai/Sample-Code/trunk/HeadsUpUI ~/Downloads/HeadsUpUI

BTW - If you are on Windows or some other platform you can find a binary download of subversion (svn) at http://subversion.apache.org

~ If you want to checkout the folder rather than simply download it try using the svn help (tldr: replace export with checkout)

Update

Regarding the comment on resuming an interrupted download/checkout. I would try running svn cleanup followed by svn update. Please search SO for additional options.



回答8:

You cannot; unlike Subversion, where each subdirectory can be checked out individually, Git operates on a whole-repository basis.

For projects where finer-grained access is necessary, you can use submodules -- each submodule is a separate Git project, and thus can be cloned individually.

It is conceivable that a Git front-end (e.g. GitHub\'s web interface, or gitweb) could choose to provide an interface for you to extract a given folder, but to my knowledge none of them do that (though they do let you download individual files, so if the folder does not contain too many files, that is an option)

Edit - GitHub actually offers access via SVN, which would allow you to do just this (as per comment). See https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away for latest instructions on how to do this



回答9:

If you truly just want to just \"download\" the folder and not \"clone\" it (for development), the easiest way to simply get a copy of the most recent version of the repository (and therefore a folder/file within it), without needing to clone the whole repo or even install git in the first place, is to download a zip archive (for any repo, fork, branch, commit, etc.) by going to the desired repository/fork/branch/commit on GitHub (e.g. http(s)://github.com/<user>/<repo>/commit/<Sha1> for a copy of the files as they were after a specific commit) and selecting the Downloads button near the upper-right.

This archive format contains none of the git-repo magic, just the tracked files themselves (and perhaps a few .gitignore files if they were tracked, but you can ignore those :p) - that means that if the code changes and you want to stay on top, you\'ll have to manually re-download it, and it also means you won\'t be able to use it as a git repository...

Not sure if that\'s what you\'re looking for in this case (again, \"download\"/view vs \"clone\"/develop), but it can be useful nonetheless...



回答10:

1.Click on this link http://kinolien.github.io/gitzip/
2.Paste link of Github folder you want to download.
3.Click on search it will shows all files for downloading.
Note:-Using search there is no need to enter token key ;)..keep it simple(y)



回答11:

Whoever is working on specific folder he needs to clone that particular folder itself , todo so please follow below steps ** --- by using SPARSE CHECKOUT --- **

Step 1: Create a directory.

Step 2: Initialize a Git repository. (git init)

Step 3: Enable Sparse Checkouts. (git config core.sparsecheckout true)

Step 4: Tell Git which directories you want (echo 2015/brand/May( refer to folder you want to work on) >> .git/info/sparse-checkout)

Step 5: Add the remote (git remote add -f origin https://jafartke.com/mkt-imdev/DVM.git)

Final Step 6: Fetch the files (git pull origin master )



回答12:

Another specific example:

Like I want to download \'iOS Pro Geo\' folder from the url

https://github.com/alokc83/APRESS-Books-Source-Code-/tree/master/%20Pro%20iOS%20Geo

and I can do so via

svn checkout https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

Note trunk in the path

Edited: (as per Tommie C\'s comment)

Yes, using export instead of checkout would give a clean copy without extra git repository files.

svn export https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

Edited: If tree/master is not there in url then Fork it and it will be there in Forked url.



回答13:

It\'s one of the few places where SVN is better than Git.

In the end we\'ve gravitated towards three options:

  1. Use wget to grab the data from GitHub (using the raw file view).
  2. Have upstream projects publish the required data subset as build artifacts.
  3. Give up and use the full checkout. It\'s big hit on the first build, but unless you get lot of traffic, it\'s not too much hassle in the following builds.


回答14:

Just to amplify the answers above, a real example from a real GitHub repository to a local directory would be:

svn ls https://github.com/rdcarp/playing-cards/trunk/PumpkinSoup.PlayingCards.Interfaces

svn export https://github.com/rdcarp/playing-cards/trunk/PumpkinSoup.PlayingCards.Interfaces  /temp/SvnExport/Washburn

Sometimes a concrete example helps clarify the substitutions proposed.



回答15:

There\'s a Python3 pip package called githubdl that can do this*:

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

The project page is here

* Disclaimer: I wrote this package.



回答16:

You can do a simple download of the directory tree:

git archive --remote git@github.com:foobar/Test.git HEAD:foo | tar xf -

But if you mean to check it out, and be able to do commits and push them back, no you can\'t do that.



回答17:

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).



回答18:

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



回答19:

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.



回答20:

Our team wrote a bash script to do this because we didn\'t want to have to install SVN on our bare bones server.

https://github.com/ojbc/docker/blob/master/java8-karaf3/files/git-download.sh

It uses the github API and can be run from the command line like this:

git-download.sh https://api.github.com/repos/ojbc/main/contents/shared/ojb-certs


回答21:

If you need to do it programatically and you don\'t want to rely on SVN, you can use GitHub API to download all the contents recursively.

For inspiration, here\'s my ruby gist: https://gist.github.com/cvengros/b2a7e82f66519d423b6f



回答22:

To export a directory from GitHub, replace \"/tree/master/\" in the directory\'s url with \"/trunk/\".

For example, to export the directory from the following URL:

https://github.com/liferay/liferay-plugins/tree/master/portlets/sample-hibernate-portlet

run the following command:

svn export https://github.com/liferay/liferay-plugins/trunk/portlets/sample-hibernate-portlet


回答23:

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.



回答24:

None of the answers helped in my situation. If you are developing for Windows, you likely don\'t have svn. In many situations one can\'t count on users to have Git installed either, or don\'t want to download entire repositories for other reasons. Some of the people that answered this question, such as Willem van Ketwich and aztack, made tools to accomplish this task. However, if the tool isn\'t written for the language you are using, or you don\'t want to install a third party library, these don\'t work.

However, there is a much easier way. GitHub has an API that allows you to download a single file or an entire directory\'s contents using GET requests. You can access a directory using https://api.github.com/repos/:owner/:repo_name/contents/:path that returns a JSON object enumerating all the files in the directory. Included in the enumeration is a link to the raw content of the file, the download_url parameter. The file can then be downloaded using that URL.

It\'s a two step process that requires the ability to make GET requests, but this can be implemented in pretty much any language, on any platform. It can be used to get files or directories.



回答25:

Use this function, the first argument is the url to the folder, the second is the place the folder will be downloaded to:

function github-dir() {
    svn export \"$(sed \'s/tree\\/master/trunk/\' <<< \"$1\")\" \"$2\"  
}


回答26:

If the directoy you want to download is a separated library, it\'s better to create an other git repo, and then to use the git submodule function.

Of course, you have to be the owner of the initial repo you want



标签: git github