anjanesh
anjanesh

Reputation: 4251

Download specific files from github in command line, not clone the entire repo

How do I download just 2 files from github using command line ?
Something in the lines of :

git fetch git://github.com/username/Project.git/file1
git fetch git://github.com/username/Project.git/file2

Upvotes: 31

Views: 193597

Answers (8)

electblake
electblake

Reputation: 2176

Plenty of great answers, so I combined them into a lil script:

GithubFileDownloader.sh

Instructions and such are in file but basically:

  1. Download GithubFileDownloader.sh
  2. chmod +x ./GithubFileDownloader.sh

Bonus step is to rename and place in $PATH like

mv GithubFileDownloader.sh /usr/local/bin/gh-dl

Example Usage

$ ./GithubFileDownloader.sh https://github.com/github/docs/blob/main/README.md
File downloaded successfully: README.md

$ cat README.md | head -2
# GitHub Docs <!-- omit in toc -->
[![Build GitHub Docs On Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new/?repo=github)

Upvotes: 1

mental_matrix
mental_matrix

Reputation: 640

Copy the specific file's raw link from GitHub.(As you open the file in Github, on the top right corner you can see the option to open the file in raw mode. Open it in raw mode and copy the URL)

Now use curl or wget command in command line to download the file.

curl -o filename raw-link-to-file

or

wget -O filename raw-link-to-file

Please note that

Upvotes: 43

kolserdav
kolserdav

Reputation: 296

Need get raw link

curl https://github.com/[user]/[package]/blob/master/package.json

and than call to link like

curl https://raw.githubusercontent.com/[user]/[package]/main/package.json

It works for me.

Upvotes: 3

Ujjawal Mandhani
Ujjawal Mandhani

Reputation: 31

I am using linux here it worked for me

Step 1 go to the file that you want to download and click on raw button

enter image description here

Step 2 copy the url from search bar

enter image description here

Step 3 run the command in your terminal

wget paste-url

enter image description here

Upvotes: -1

ZDL-so
ZDL-so

Reputation: 486

In new UI interface(about June 2020), if your file url is

https://github.com/StellarCN/scp_zh/blob/master/fonts/SimHei.ttf

then

wget https://github.com/StellarCN/scp_zh/blob/master/fonts/SimHei.ttf?raw=true

Upvotes: 13

Gyumeijie
Gyumeijie

Reputation: 172

You can try github-files-fetcher, it is a command line tool which downloads a single folder or file from a GitHub repo.

Given the example above, you can use the following command to fetch the two specific files from github:

fetcher --url="git://github.com/username/Project.git/file1" 
fetcher --url="git://github.com/username/Project.git/file2" 

Think a more real scenario: you were visiting the following webpage page and wanna download the async subdirectory alone.

https://github.com/reduxjs/redux/tree/master/examples

sorry for not being allowed to post images.

With The github-files-fetcher, you should first copy the url of that page, which is https://github.com/reduxjs/redux/tree/master/examples/async, and then run the command below in command line:

fetcher --url=https://github.com/reduxjs/redux/tree/master/examples/async

Upvotes: 1

dbrumann
dbrumann

Reputation: 17166

If you go to the page and view the links provided by "raw" (in the top left corner, when viewing the file). You will see, that you can access it by:

https://github.com/username/repository/raw/$changeset_hash/path/to/file

Instead of $changeset_hash you can also provide a branch (e.g. master) or tag.

You can retrieve the raw file using something like wget.

Accessing a single file directly from a .git-repository is not possible (as far as I know), because of how the data is stored.

edit: When you want to access a file from a private repo, you first have to create an access token with the appropriate permissions in your account settings. Instead of calling the url above you can then use github's API to access the content of a file. Be sure to use the Accept-header for custom media types to get the raw data. This might look something like this:

curl \
  -H 'Authorization: token $YOUR_TOKEN' \
  -H 'Accept: application/vnd.github.v3.raw' \
  -O \
  -L 'https://api.github.com/repos/:owner/:repo/contents/:path'

The -O will save the contents in a local file with the same name as the remote file name. For easier use you can wrap it in a script. @Chris_Withers suggested an edit with a nice python snippet that unfortunately got rejected as to big of a change to the answer.

Upvotes: 40

vpatil
vpatil

Reputation: 3476

git checkout  

E.g:

git checkout master~2 file1

(git checkout --help for help)

Upvotes: 3

Related Questions