Category Archives: git

Opening github in your browser from the terminal

Here is my first ever bash function! When you run it from the terminal it opens the github page in your browser for the git repository you are currently in. In keeping with bash best practices it has a short simple name: gh. I find myself doing this quite a lot as I prefer the github log and history views to the gitx or command line views.

# Opens the github page for the current git repository in your browser
function gh() {
  giturl=$(git config --get remote.origin.url)
  if [ "$giturl" == "" ]
     echo "Not a git repository or no remote.origin.url set"
     exit 1;

  branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
  branch="(unnamed branch)"     # detached HEAD
  open $giturl

Just type gh:
Opening github from the terminal

Boom! github!
The github page for the repository

How it works

It grabs the remote url via the git config command: Next it does some simple string replacements to get a github http url: Finally it open the github url in the default browser using the MacOS open command. As it uses the open command I imagine it only works on MacOSs.