58

I wished to download the mysite folder from this link: https://github.com/username/repository/master/

C1sc0
  • 1,469
  • 4
  • 28
  • 30
Gangesh
  • 673
  • 1
  • 6
  • 10
  • 10
    Possible duplicate of [Download a single folder or directory from a GitHub repo](https://stackoverflow.com/questions/7106012/download-a-single-folder-or-directory-from-a-github-repo) – Helen Jul 21 '17 at 09:37
  • 2
    Go to [DownGit](https://downgit.github.io) > Enter GitHub Folder URL > Download (no command, no tool, no fuss!) – Minhas Kamal May 13 '20 at 15:47
  • 2
    github UI sucks. should have download folder option. – JGFMK Dec 13 '20 at 20:09

10 Answers10

48

You can download a file/folder from github

Simply use: svn export <repo>/trunk/<folder>

Ex: svn export https://github.com/lodash/lodash/trunk/docs

Note: You may first list the contents of the folder in terminal using svn ls <repo>/trunk/folder

(yes, that's svn here. apparently in 2016 you still need svn to simply download some github files)

Vishwajith.K
  • 83
  • 2
  • 10
Anona112
  • 3,534
  • 4
  • 19
  • 30
26

You can use Github Contents API to get an archive link and tar to retrieve a specified folder.

Command line:

curl https://codeload.github.com/[owner]/[repo]/tar.gz/master | \ tar -xz --strip=2 [repo]-master/[folder_path]


For example,
if you want to download examples/with-apollo/ folder from zeit/next.js, you can type this:

curl https://codeload.github.com/zeit/next.js/tar.gz/master | \
  tar -xz --strip=2 next.js-master/examples/with-apollo
chuyik
  • 778
  • 7
  • 12
  • 3
    Nice! It's also optional with **WGET**, and with the regular **github archive url**: `wget -O - https://github.com/zeit/next.js/archive/master.tar.gz | tar -xz --strip=2 "next.js-master/examples/with-apollo"` – Noam Manos May 21 '20 at 12:02
  • 1
    zip might works as well. `wget -c https://github.com/user/project/archive/master.zip` – Nick Dong Jul 18 '20 at 06:29
  • 1
    This option is rubbish. If you only what images - it downloads whole repo first then trims it. Makes it less painful to individually download 15 images manually! – JGFMK Dec 13 '20 at 19:59
19

Use GitZip online tool. It allows to download a sub-directory of a github repository as a zip file. No git commands needed!

gdrt
  • 2,843
  • 3
  • 36
  • 53
  • 3
    perfect solution – dvdmn Aug 10 '19 at 15:28
  • 2
    Why does it need write access to all my public repos? Isn't this a security risk? – Jared Thirsk Oct 22 '19 at 05:24
  • @JaredThirsk, but it doesn't. Can you tell us when did it happen in your case? – gdrt May 15 '20 at 09:54
  • @gdrt When I click Get Token: Normal, Github asks: "This application will be able to read and write all public repository data. This includes the following: - Code, ..." – Jared Thirsk May 18 '20 at 02:53
  • @JaredThirsk, you should provide API Access Token if the repository isn't public. If it is public, simply providing repository URL and clicking Download button will do it. – gdrt May 18 '20 at 11:29
  • @gdrt Ok, I see. It would be nice if there wasn't a popup expanded by default when going to that page, as well as the message "We recommended to get the token first and then do your actions." – Jared Thirsk May 19 '20 at 12:20
  • 1
    Rare to find such an easy solution to what you want to do. This really helped me. Thanks! – ZeroKelvin Dec 10 '21 at 21:31
10

There is a button Download ZIP. If you want to do a sparse checkout there are many solutions on the site. For example here.

C1sc0
  • 1,469
  • 4
  • 28
  • 30
  • 2
    download everything as a zip...then unzip, delete what you don't want, and rezip it and scp it to where you want it to go... – Monica Heddneck Jan 12 '18 at 23:41
  • Or just simply download the whole repository and browse you desired files without installing or configuring any tool. (anyway your comment is a duplicate) – C1sc0 Feb 24 '18 at 14:16
  • 3
    This should not be the accepted answer. To be specific, "You can't download only a folder or a file," is wrong. See below for various correct solutions. – colemars Aug 30 '19 at 19:15
  • Thank you for your suggestion, I will update the answer. I don't think the solutions below are correct, they just work. You can use `git` to download a sub-folder from a repository. Anyway, the repository hosting site github.com doesn't provide a way to do so without using any external tools. – C1sc0 Aug 31 '19 at 10:38
10

How to download a specific folder from a GitHub repo

Here a proper solution according to this post:

  • Create a directory

     mkdir github-project-name 
     cd github-project-name
    
  • Set up a git repo

     git init
     git remote add origin <URL-link of the repo>
    
  • Configure your git-repo to download only specific directories

     git config core.sparseCheckout true # enable this
    
  • Set the folder you like to be downloaded, e.g. you only want to download the doc directory from https://github.com/project-tree/master/doc

     echo "/absolute/path/to/folder" > .git/info/sparse-checkout 
    

    E.g. if you only want to download the doc directory from your master repo https://github.com/project-tree/master/doc, then your command is echo "doc" > .git/info/sparse-checkout.

  • Download your repo as usual

     git pull origin master
    
abu_bua
  • 1,133
  • 12
  • 19
  • The best answer of all! However, I believe there should no be "-f" in "git remote add origin", since "-f" means a immediate fetch from server, which should be avoided since only the "sparse" path is wanted! – Robert Jun 24 '20 at 14:36
  • Awesome answer! – canmustu Jun 24 '21 at 12:20
  • 2
    in my case, I could see from `git pull origin`'s verbose that doing this downloaded the entire repository; but retained only the folder specified in `.git/info/sparse-checkout`. and using `https://downgit.github.io/` did it for me. – Naveen Reddy Marthala Sep 18 '21 at 05:39
1

Dependency: cURL and 7-Zip.

curl {url for downloading zip file} | 7z a -tzip {project name}-{branch name}/{folder path in that branch}

for example:

curl https://github.com/hnvn/flutter_shimmer/archive/master.zip | 7z a -tzip flutter_shimmer-master/examples
Kalpesh Kundanani
  • 4,622
  • 3
  • 15
  • 27
1

If you want to automate the steps, the suggestions here will work only to an extent.

I came across this tool called fetch and it worked quite fine for me. You can even specify the release. So, it takes a step to download and set it as executable, then fetch the required folder:

curl -sSLfo ./fetch \
https://github.com/gruntwork-io/fetch/releases/download/v0.3.12/fetch_linux_amd64
chmod +x ./fetch
./fetch --repo="https://github.com/foo/bar" --tag="${VERSION}" --source-path="/baz" /tmp/baz
matrik
  • 76
  • 3
  • My vote goes to matrik. This fetch utility is exactly what I was looking for: https://github.com//gruntwork-io/fetch. Its a command line tool that not only fits the use case of the OP, but also provides many other useful features and switches for specifying exactly what you are after. – nelsestu Aug 30 '21 at 18:21
1

There exists also a nice browser extension which allows to dl files or folders

abu_bua
  • 1,133
  • 12
  • 19
-2

You can download the complete folder under Clone or Download options (Git URL or Download Zip)

  1. There is a button of Download Zip

  2. By using command you can download the complete folder on your machine but for that you need git on your machine. You can find the Git url uner

    git clone https://github.com/url

Community
  • 1
  • 1
VIKAS KOHLI
  • 7,314
  • 3
  • 48
  • 49
-6

You can also just clone the repo, after cloning is done, just pick the folder or vile that you want. To clone:

git clone https://github.com/somegithubuser/somgithubrepo.git

then go to the cloned DIR and find your file or DIR you want to copy.

Stephen Rauch
  • 44,696
  • 30
  • 102
  • 125
user2963512
  • 101
  • 4