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 12:02

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.

查看更多
旧时光的记忆
3楼-- · 2019-01-01 12:03

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

查看更多
像晚风撩人
4楼-- · 2019-01-01 12:04

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

查看更多
余生请多指教
5楼-- · 2019-01-01 12:05

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.

查看更多
零度萤火
6楼-- · 2019-01-01 12:05

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
查看更多
柔情千种
7楼-- · 2019-01-01 12:08

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
查看更多
登录 后发表回答