Skip to content

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.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

hotloo commented Sep 23, 2013

This is pretty awesome!

@kesor

This comment has been minimized.

Copy link

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.

Copy link

adamcbrewer commented Sep 23, 2013

Cheers for this. Massive help.

@mathiasbynens

This comment has been minimized.

Copy link

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.

Copy link

DavidSouther commented Sep 23, 2013

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

@SEJeff

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

rsurjano commented Apr 19, 2018

@eeroan tested on Macosx but failed on linux

@MagicMicky

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

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
@kguidonimartins

This comment has been minimized.

Copy link

kguidonimartins commented Apr 14, 2019

This works on Ubuntu:

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

This comment has been minimized.

Copy link

SmartManoj commented Jun 17, 2019

I get from git config branch.master.remote

@imambungo

This comment has been minimized.

Copy link

imambungo commented Jul 25, 2019

xdg-open https://github.$(git config remote.origin.url | cut -f2 -d.)

The above command doesn't work well in zsh if you save it as an alias. Save it as a function instead:

gh() {
    xdg-open https://github.$(git config remote.origin.url | cut -f2 -d.)
}
@rishub

This comment has been minimized.

Copy link

rishub commented Aug 14, 2019

@imambungo why exactly doesn't it work well on zsh as an alias?

@imambungo

This comment has been minimized.

Copy link

imambungo commented Aug 15, 2019

@rishub Sorry, I don't know exactly why. When I save it as an alias, whenever I type gh in a git repo from zsh, it will open the GitHub URL for $HOME directory/repo. My $HOME directory has git initiated to save my dotfiles, and I don't know if this issue happens because of that or not. And I'm not sure why the issue doesn't happen when I save gh as a function. I'd like to know from someone who uses zsh and have their $HOME directory used as a git repo if they also face this issue.

@alex-r-bigelow

This comment has been minimized.

Copy link

alex-r-bigelow commented Sep 26, 2019

... and a version for anyone using WSL:

alias gh="explorer.exe https://github.$(git config remote.origin.url | cut -f2 -d. | tr ':' /)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.