Sometimes you just need to get a single file out of Github. You don’t care about the whole repo. Maybe you just need a snippet of code, or maybe just one language version, or whatever.

You can, of course, copy and paste with a UI clipboard, but that can be error prone and isn’t scriptable. You might think you could just curl <url> to the page, but you would just get the HTML for the page, rather than the file contents themselves.

Github actually can generate a number of views for your file. They follow this syntax

<site>/<owner>/<reponame>/<view>/<branch>/<pathToFile>

where the legal values for view are:

  • blob (basic HTML page)
  • raw (actual contents)
  • blame (annotated HTML page)

For example,

https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
https://github.com/git/git/raw/master/contrib/completion/git-completion.bash

where https://github.com is the site, git is the owner, git is the repo, blob or raw is the view, master is the branch, and contrib/completion/git-completion.bash is the file

Since what you want is the actual file, rather than an HTML document with information and links to the file, select the “raw” option. Because you’re now dealing with actual content, the actual URL may redirect, so be sure to use curl’s -L option to interpret the HTTP Location header.

curl -L https://github.com/git/git/raw/master/contrib/completion/git-completion.bash