Skip to main content

Connecting Visual Studio to Github

I learned in the past, that it is always best to connect workspaces to source control, even for experiments and learning projects. Sometimes it’s because you’ve screwed something up terribly and you need to retreat to a last known good; sometimes it’s because you want to continue on a machine that’s different from the one you were working on earlier.

So, before I started learning react/redux (or anything else), I wanted to get my IDE connected up to Github, where I keep my source code. I’ve done this plenty of times on Linux and Mac (which is basically Linux), but never had to do it on Windows. Turns out it is a bit rough, but possible.

I am using Visual Studio Code (VSC) as my IDE for this kind of work for now. That comes out of the box with a git SCM extension, but the extension does not support using SSH AuthN to a remote server like Github. I prefer to use SSH because that’s what I know and use on other OSes. To get that to work with VSC, I first start a git-bash shell from Git for Windows, whose SSH environment is configured following the instructions in a gist from github user bsara. I then simply start the VSC executable from the shell using the command code so it inherits the SSH environment and can work with Github.

Apart from the small annoyance of having to start a shell to start VSC, this seems to work great and I am now happily interacting with my Github account directly from VSC.

Getting a single file out of Github

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