Create a gist now

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@jm3

jm3 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"

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"
@kimpepper

This comment has been minimized.

Show comment
Hide comment
@kimpepper

kimpepper Sep 23, 2013

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

kimpepper commented Sep 23, 2013

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

@carpeliam

This comment has been minimized.

Show comment
Hide comment
@carpeliam

carpeliam Sep 23, 2013

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\//'\`"

carpeliam commented Sep 23, 2013

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

This comment has been minimized.

Show comment
Hide comment
@hotloo

hotloo Sep 23, 2013

This is pretty awesome!

hotloo commented Sep 23, 2013

This is pretty awesome!

@kesor

This comment has been minimized.

Show comment
Hide comment
@kesor

kesor Sep 23, 2013

I'll just continue the ugly ...

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

kesor commented Sep 23, 2013

I'll just continue the ugly ...

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

This comment has been minimized.

Show comment
Hide comment
@adamcbrewer

adamcbrewer Sep 23, 2013

Cheers for this. Massive help.

adamcbrewer commented Sep 23, 2013

Cheers for this. Massive help.

@mathiasbynens

This comment has been minimized.

Show comment
Hide comment
@mathiasbynens

mathiasbynens Sep 23, 2013

@kesor: Did you mean:

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

mathiasbynens commented Sep 23, 2013

@kesor: Did you mean:

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

This comment has been minimized.

Show comment
Hide comment
@DavidSouther

DavidSouther Sep 23, 2013

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

DavidSouther commented Sep 23, 2013

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

@SEJeff

This comment has been minimized.

Show comment
Hide comment
@SEJeff

SEJeff 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"

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"
@jmervine

This comment has been minimized.

Show comment
Hide comment
@jmervine

jmervine Sep 23, 2013

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.

jmervine commented Sep 23, 2013

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

This comment has been minimized.

Show comment
Hide comment
@drewbo

drewbo 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"

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"
@elliott-beach

This comment has been minimized.

Show comment
Hide comment
@elliott-beach

elliott-beach Aug 16, 2016

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

elliott-beach commented Aug 16, 2016

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

@sks

This comment has been minimized.

Show comment
Hide comment
@sks

sks Mar 14, 2018

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

sks commented Mar 14, 2018

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

This comment has been minimized.

Show comment
Hide comment
@eeroan

eeroan Mar 20, 2018

Navigate directly to current branch:

open $(git config remote.origin.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")/tree/$(git symbolic-ref --quiet --short HEAD )

eeroan commented Mar 20, 2018

Navigate directly to current branch:

open $(git config remote.origin.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")/tree/$(git symbolic-ref --quiet --short HEAD )
@rsurjano

This comment has been minimized.

Show comment
Hide comment
@rsurjano

rsurjano Apr 19, 2018

@eeroan tested on Macosx but failed on linux

rsurjano commented Apr 19, 2018

@eeroan tested on Macosx but failed on linux

@MagicMicky

This comment has been minimized.

Show comment
Hide comment
@MagicMicky

MagicMicky Jul 9, 2018

open $(git config remote.origin.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")/tree/$(git symbolic-ref --quiet --short HEAD )/$(git rev-parse --show-prefix)

Added the current directory to @eeroan alias (osx only)

MagicMicky commented Jul 9, 2018

open $(git config remote.origin.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")/tree/$(git symbolic-ref --quiet --short HEAD )/$(git rev-parse --show-prefix)

Added the current directory to @eeroan alias (osx only)

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