Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Open GitHub URL for current directory/repo...
alias gh="open \`git remote -v | grep git@github.com | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`"

jm3 commented Sep 23, 2013

Nice! For our 140-character world, here’s a shorter version that saves 23 characters:

alias gh="open `git remote -v | grep fetch | awk '{print $2}' | sed 's/git@/http:\/\//' | sed 's/com:/com\//'`| head -n1"

Use the "hub" gem, and then you have git browse which does the same thing. https://github.com/github/hub

Modified to open my non-github repos too (in my case, gitorious):

alias gh="open \`git remote -v | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/git@/http:\/\//' -e's/\.git$//' | sed -E 's/(\/\/[^:]*):/\1\//'\`"

hotloo commented Sep 23, 2013

This is pretty awesome!

kesor commented Sep 23, 2013

I'll just continue the ugly ...

alias gh="open https://github.$(git config remote.origin.url | cut -f2 -d.)"

Cheers for this. Massive help.

@kesor: Did you mean:

alias gh="open https://github.$(git config remote.origin.url | cut -f2 -d. | tr ':' /)"

@mathiasbynens There are many ways to specify a remote. Don't blindly copypasta :)

SEJeff commented Sep 23, 2013

@j3m: This version works a bit better with a shorter shell pipeline. It works with ssh:// git git:// remotes as well

alias gh="open `git remote -v | awk '/fetch/{print $2}' | sed -re 's#(git@|git://)#http://#' -e 's@com:@com/@'`| head -n1"

From the original, to work with both Mac and Linux:

alias gh="if [ \`uname -s\` = \"Darwin\" ]; then browser=\"open\"; else browser=\"xdg-open\"; fi; $browser \`git remote -v | grep git@github.com | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`"

Or simply replace open with xdg-open.

drewbo commented Jun 2, 2015

@SEJeff 👍 changed -re to -Ee for Mac OS X

alias gh="open `git remote -v | awk '/fetch/{print $2}' | sed -Ee 's#(git@|git://)#http://#' -e 's@com:@com/@'`| head -n1"

e-beach commented Aug 16, 2016 edited

If you only need it to work for Mac with HTTPS, you can just do
open $(git config remote.origin.url).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment