How to download a file from private GitHub repo via the command line

I'd like to find an easier way to grab a file from a private GitHub repository.

I managed to download a file via curl by creating a personal access token on GitHub, and then using it like this:

curl https://[email protected]/accountName/repoName/branchName/fileName.txt

This works for me, however I was wondering if there's an easier way, perhaps using SSH? I wouldn't mind having to type my SSH password as it seems more convenient than creating an access token. I can already authenticate successfully on GitHub:

$ ssh -T [email protected]
# Attempts to ssh to GitHub

However, when I try using curl with an SSH URL, I get this error:

curl ssh://[email protected]:accountName/repoName/branchName/fileName.txt
curl: (3) URL using bad/illegal format or missing URL

I tried some other methods described in this article, to no avail.

Any ideas?


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.)