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.

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.

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.

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.

hotloo commented Sep 23, 2013

This is pretty awesome!

@kesor

This comment has been minimized.

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.

adamcbrewer commented Sep 23, 2013

Cheers for this. Massive help.

@mathiasbynens

This comment has been minimized.

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.

DavidSouther commented Sep 23, 2013

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

@SEJeff

This comment has been minimized.

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.

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.

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.

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.

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.

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.

rsurjano commented Apr 19, 2018

@eeroan tested on Macosx but failed on linux

@MagicMicky

This comment has been minimized.

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)

@mohitmun

This comment has been minimized.

mohitmun commented Aug 30, 2018

taking inspiration from all scripts here,

gh(){
  open $(git config remote.origin.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")/$1$2
}

# Open current branch
alias ghb='gh tree/$(git symbolic-ref --quiet --short HEAD )'

# Open current directory/file in current branch
alias ghbf="gh tree/$(git symbolic-ref --quiet --short HEAD )/$(git rev-parse --show-prefix)"

# Open current directory/file in master branch
alias ghf='gh tree/master/$(git rev-parse --show-prefix)'

(works on osx)

@llk23r

This comment has been minimized.

llk23r commented Sep 15, 2018

changing the sed to the following expression worked for me on osx. It removes the trailing.git from the remote origin url.

gh(){open $(git config remote.origin.url | sed 's/.\{4\}$//')/$1$2}
@abranhe

This comment has been minimized.

abranhe commented Sep 28, 2018

Simplest way, using openup

$ npm install -g openup

then inside of a git repository, just

$ openup

API of openup:

git remote -v | awk '/origin.*push/ {print$2}' | xargs open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment