gh - Open GitHub from the Command Line

Main Thread 1 min read

At work we used git backed by GitHub. After every git push I open the browser, go to GitHub, and navigate to the branch.

While only a few clicks, I repeat this process many times a day. In lazy developer fashion I wanted to automate this process.

A Google search for open GitHub from command line yielded two promising results.

First, hub, which not only opened GitHub from the command line but wrapped git entirely to provide even more features. hub looked awesome, but went well-beyond my needs.

The second result was a bash function that generates your GitHub URL and uses the Mac OS open command to launch the browser.

Unfortunately it didn't work. Being just a few lines of code, I decided to fix the script. To honor the original author, I kept the same command name - gh.

By simply typing gh I can open the current branch in my repository on GitHub.

I also extended the original script with optional parameters for remote and branch. It follows a similar format as git push:

gh [remote-name] [branch-name]

These default to origin and the current checked out branch, respectively.

Now I can open any repository and branch on GitHub from the command line with:

gh origin dev

I shared gh on GitHub. Then Paul Irish turn it into an even better npm script as git open.

Find this interesting? Let's continue the conversation on Twitter.