Download A File From Github

What is the most efficient mechanism (in respect to data transferred and disk space used) to get the contents of a single file from a remote git repository?

  1. Download A File From Github Command Line
  2. Download A File From Github To Linux

Download files from Github without Git using PowerShell May 31, 2017 ~ MSAdministrator Have you ever needed to download code or a repository from Github, but didn’t want to download and install Git on a machine, create an SSH key, etc. Aug 12, 2016 - If you want a faster way to download a single file, or the contents of a single file, Github Plus is a Chrome extension you might find exceptionally.

So far I've managed to come up with:

This still seems overkill.

What about getting multiple files from the repo?

Kara
4,10610 gold badges44 silver badges53 bronze badges
Theozaurus

19 Answers

in git version 1.7.9.5 this seems to work to export a single file from a remote

This will cat the contents of the file README.md.

nhahtdh
48.8k13 gold badges100 silver badges135 bronze badges
Yisrael DovYisrael Dov
1,6981 gold badge13 silver badges9 bronze badges

Following on from Jakub's answer. git archive produces a tar or zip archive, so you need to pipe the output through tar to get the file content:

Will save a copy of 'filename' from the HEAD of the remote repository in the current directory.

The :path/to/directory part is optional. If excluded, the fetched file will be saved to <current working dir>/path/to/directory/filename

In addition, if you want to enable use of git archive --remote on Git repositories hosted by git-daemon, you need to enable the daemon.uploadarch config option. See https://kernel.org/pub/software/scm/git/docs/git-daemon.html

DavidRR
10k11 gold badges64 silver badges139 bronze badges
Robert KnightRobert Knight

If there is web interface deployed (like gitweb, cgit, Gitorious, ginatra), you can use it to download single file ('raw' or 'plain' view).

If other side enabled it, you can use git archive's '--remote=<URL>' option (and possibly limit it to a directory given file resides in), for example:

Jakub NarębskiJakub Narębski
223k55 gold badges199 silver badges222 bronze badges

Not in general but if you are using Github:

For me wget to the raw url turned out to be the best and easiest way to download one particular file.

Open the file in the browser and click on 'Raw' button. Now refresh your browser, copy the url and do a wget or curl on it.

wget example:

wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py

Curl example:

curl 'https://example.com/raw.txt' > savedFile.txt

Zitrax
9,53912 gold badges69 silver badges82 bronze badges
abcabc
9,10824 gold badges92 silver badges151 bronze badges

To export a single file from a remote:

This will download the file README.md to your current directory.

If you want the contents of the file exported to STDOUT:

You can provide multiple paths at the end of the command.

KoushaKousha

It looks like a solution to me: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

git show HEAD~4:index.html > local_file

where 4 means four revision from now and ~ is a tilde as mentioned in the comment.

Mars RobertsonMars Robertson
8,0038 gold badges50 silver badges78 bronze badges
Steven PennySteven Penny

A nuanced variant of some of the answers here that answers the OP's question:

Willem van KetwichWillem van Ketwich
2,3194 gold badges30 silver badges44 bronze badges

If you repository supports tokens (for example GitLab) then generate a token for your user then navigate to the file you will download and click on RAW output to get the URL. To download the file use:

panticz.depanticz.de

Yisrael Dov's answer is the straightforward one, but it doesn't allow compression. You can use --format=zip, but you can't directly unzip that with a pipe command like you can with tar, so you need to save it as a temporary file. Here's a script:

This works with directories too.

Community
naught101naught101
10.5k12 gold badges67 silver badges114 bronze badges

For single file, just use wget command.

First, follow the pic below to click 'raw' to get the url, otherwise you will download code embedded in html.

Then, the browser will open a new page with url start with https://raw.githubusercontent.com/...

From

just enter the command in the terminal:

A while the file will put in your folder.

Download A File From Github Command Line

malajisimalajisi

If your Git repository hosted on Azure-DevOps (VSTS) you can retrieve a single file with Rest API.

The format of this API looks like this:

For example:

Shayki AbramczykShayki Abramczyk
6,9669 gold badges18 silver badges34 bronze badges

I solved in this way:

If you want, you could replace 'BranchName' for 'HEAD'

matiasmascamatiasmasca

I use curl, it works with public repos or those using https basic authentication via a web interface.

Ftp

curl -L --retry 20 --retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u USER:PASSWORD

I've tested it on github and bitbucket, works on both.

JasonSJasonS
4,3173 gold badges33 silver badges51 bronze badges

If you want to get a file from a specific hash + a remote repository I've tried git-archive and it didn't work.

You would have to use git clone and once the repository is cloned you would have then to use git-archive to make it work.

I post a question about how to do it more simpler in git archive from a specific hash from remote

Community
Fernanda MartinsFernanda Martins

for bitbucket directly from browser (I used safari...) right-click on 'View Raw' and choose 'Download Linked File':

ingcontiingconti

Download A File From Github To Linux

6,5011 gold badge36 silver badges29 bronze badges

If you don't mind cloning the entire directory, this small bash/zsh function will have the end result of cloning a single file into your current directory (by cloning the repo into a temp directory and removing it afterwards).

Pro: You only get the file you want

Con: You still have to wait for the whole repo to clone

Christopher ShrobaGithubChristopher Shroba
2,8321 gold badge16 silver badges42 bronze badges
Oliver PearmainOliver Pearmain
11.3k8 gold badges67 silver badges69 bronze badges

Related to @Steven Penny's answer, I also use wget. Furthermore, to decide which file to send the output to I use -O .

If you are using gitlabs another possibility for the url is:

Unless you have the certificate or you access from a trusted server for the gitlabs installation you need --no-check-certificate as @Kos said. I prefer that rather than modifying .wgetrc but it depends on your needs.

If it is a big file you might consider using -c option with wget. To be able to continue downloading the file from where you left it if the previous intent failed in the middle.

Hector LlorensHector Llorens