Open Github Repos From the Command Line

less than 1 minute read

If you’re ever sitting in a Git repository in the shell and want to view it on Github, here’s some quick laziness.

Bash

alias view-on-gitub=$'open `git config --get remote.origin.url | awk -F: \'{ print "https://github.com/"$2}\'`'

(The $ after the = is an example of using Bash’s ANSI C quoting mechanism.)

(T)CSH

alias view-on-gitub 'open `git config --get remote.origin.url | awk -F: ' \' '{ print "https://github.com/"$2}' \' '`'

(CSH single quote escaping is even more convoluted…)

Basically, we grab the remote URL from with git config, strip out the repo path and slap https://github.com/ in front of it with awk, and then use open to feed the new URL the default web browser.

This makes a couple presumptions:

  • You are in a Git repo.
  • You are using SSH style URLs.
  • open is OS X specific. On Linux you’d want xdg-open or gnome-open, on Windows, start, under Cygwin, cygstart.

Making it smarter is left as an exercise to the reader.

Comments