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
Answer from mental_matrix on Stack OverflowCopy 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
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.
Videos
The shortest way that seems to be what you want would be git clone https://github.com/user/repository --depth 1 --branch=master ~/dir-name. This will only copy the master branch, it will copy as little extra information as possible, and it will store it in ~/dir-name.
This will clone the files into new directory it creates:
git clone [email protected]:whatever NonExistentNewFolderName
- Go to the file you want to download.
- Click it to view the contents within the GitHub UI.
- In the top right, right click the
Rawbutton. - Save as...
Git does not support downloading parts of the repository. You have to download all of it. But you should be able to do this with GitHub.
When you view a file it has a link to the "raw" version. The URL is constructed like so
https://raw.githubusercontent.com/user/repository/branch/filename
By filling in the blanks in the URL, you can use Wget or cURL (with the -L option, see below) or whatever to download a single file. Again, you won't get any of the nice version control features used by Git by doing this.
Update: I noticed you mention this doesn't work for binary files. You probably shouldn't use binary files in your Git repository, but GitHub has a download section for each repository that you can use to upload files. If you need more than one binary, you can use a .zip file. The URL to download an uploaded file is:
https://github.com/downloads/user/repository/filename
Note that the URLs given above, from the links on github.com, will redirect to raw.githubusercontent.com. You should not directly use the URL given by this HTTP 302 redirect because, per RFC 2616: "Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests."
First of all, curl says "bad/illegal format" because you're mixing the URL-style and rsh-style address formats. Traditionally, Git accepts both for git clone, but only the latter puts a : between host and path – whereas in URLs, all paths start with a /. For example, the rsh/scp-style address [email protected]:foo/bar would be written as the URL ssh://[email protected]/foo/bar, just like HTTP URLs.
SSH is not a file transfer protocol on its own – it's more like TLS, something that can carry various file transfer protocols such as SFTP or scp or rsync (much like TLS can carry HTTP). Giving curl an ssh:// URL is meaningless1, but you could give it an sftp:// one to retrieve a file over SFTP. (Note how the article that you linked also specifically uses SFTP.)
However, GitHub does not provide SFTP access; the only thing allowed over SSH connections to GitHub is the Git protocol. That's not something you can access with curl, only with git clone.
So if you must use SSH, then your only option with GitHub is to actually clone the repository via Git. (It is possible to reduce the download size using --depth= or --filter= options, but it still ends up being a whole repository and not just the individual file.)
1 (Git uses ssh:// URLs but the meaning is clear from context – it's the Git protocol, but tunnelled over SSH. Git doesn't use SFTP.)
first of all you need a API access token from github how to create it:
- go to github.com and click on your profile picture at top right
- scroll down and click settings on the side bar which is shown
- scroll down and click on developer settings on left sidebar
- click on Personal Access Token(menu bar)
- click on tokens(classic)
- then click on Generate New Token(menu bar)
- then click on generate new token(classic)
- write the information of the token(name, expires date) and be careful! allow all of the access which is shown on the page! if you don't the API shouldn't work
- copy your API key
- write this code with the token you got copy
import requests
from requests.structures import CaseInsensitiveDict
GH_PREFIX = "https://raw.githubusercontent.com"
ORG = "GITHUB_USERNAME"
REPO = "YOUR_REPOSITORY_NAME"
BRANCH = "YOUR_REPOSITORY_BRANCH"
FOLDER = "THE_FOLDER_WHICH_INCLUDE_TEST.txt"
FILE = "THE_FILE_YOU_WANT_TO_ACCESS(TEST.txt)"
url = GH_PREFIX + "/" + ORG + "/" + REPO + "/" + BRANCH + "/" + FOLDER + "/" + FILE
headers = CaseInsensitiveDict()
headers["Authorization"] = "token " + "YOUR_API_ACCESS_TOKEN_HERE"
r = requests.get(url, headers=headers, stream=True)
first = str(r.content).replace("b'", "")
second = first.replace("\\r", "")
third = second.replace("\\n'", "")
result = third.replace("'", "")
print(result)
i hope this helps you
It does work, if you use the correct URL.
For a GitHub repo, there's a zip at https://github.com/<user>/<repo>/archive/<branch>.zip, so you can download it with:
wget https://github.com/<user>/<repo>/archive/<branch>.zip
This downloads the zipped repo for a given branch. Note that you can also replace the branch by a commit hash.
Using cURL
curl -L -O https://github.com/<user>/<repo>/archive/<branch>.zip
cURL's -L flag follows redirects - it's a default in wget. -O is advised by Phil Gibbins in a comment.
Download a .tgz instead of .zip
You can also download a tarball with:
wget https://github.com/<user>/<repo>/archive/<branch>.tar.gz
From the comments I saw you actually speak about GitHub.
It won't work like this because:
Downloading a project on GitHub causes the GitHub server to first pack your project as zip and than forwarding you to a temporary link where you get your zip ..
this link will only work for a certain time and than GitHub will delete your zip file from their servers..
So what you get with wget is just the html page which would forward you as soon as your zip file is generated.
As suggested use
git clone http://github.com/<yourRepoLink> <optional local path where to store>
to download the git repo ... If for some reason (e.g. for transfer it to others) you need it explicitly as zip you still could pack it after cloning is finished..