Open a Github Repo Quickly from the Command Line

If you are a sysadmin or developer that uses a lot of repos throughout the day, and you use Github, this little snippet will come in handy. No more opening a browser window and fumbling around trying to navigate to your repo. Just type gh anywhere inside your project path and a browser window will open your repo on Github.


# GH
# by Brock Angelo
# for Mac OSX & Unix
# https://gist.github.com/1917716
# Description: when you are on the command line and want to open your project in Github, just type "gh". Latest version opens the current branch on github as well.
#
# SETUP:
# 1. Copy this snippet to ~/bin/gh
# 2. Make it executable by running: chmod +x ~/bin/gh
# 3. From the command line, inside your project, type gh and the browser window will open.
BRANCH=$(git branch | grep "*" | awk '{print $2}')
git remote -v | grep fetch | sed 's/\(.*github.com\)[:|/]\(.*\).git (fetch)/\2/' | awk -v BRANCH=$BRANCH {'print "https://github.com/" $1 "/tree/" BRANCH'} | xargs open

view raw

gh.sh

hosted with ❤ by GitHub

For this to work, you’ll need to do two things:

  1. Copy the snippet to ~/bin/gh
  2. Make it executable by running: chmod +x ~/bin/gh
  3. Reload or restart your terminal, browse into a Github project, type gh and boom!

Let me know if you try it...