Skip to content

Instantly share code, notes, and snippets.

Last active May 18, 2023 16:02
Star You must be signed in to star a gist
What would you like to do?
Open GitHub URL for current directory/repo...
alias gh="open \`git remote -v | grep | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`"
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).

Copy link

sks commented Mar 14, 2018

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

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 )

Copy link

@eeroan tested on Macosx but failed on linux

Copy link

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)

Copy link

mohitmun commented Aug 30, 2018

taking inspiration from all scripts here,

  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)

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}

Copy link

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

Copy link

This works on Ubuntu:

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

Copy link

I get from git config branch.master.remote

Copy link

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.)

Copy link

rishub commented Aug 14, 2019

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

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.

Copy link

... and a version for anyone using WSL:

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

Copy link

lmatter commented Dec 2, 2019

I arrived here looking for a way to convert the github URL into an argument for go get. Shamelessly copied from these examples into such a script.

Copy link

eytanhanig commented Feb 7, 2020

Here's what I'm using right now, which has the following advantages:

  1. It supports both SSH and HTTPS clones of GitHub repos.
  2. You can specify the file or directory to open in GitHub, otherwise it defaults to the current directory.
  3. After specifying a file/directory you can also specify the branch to use, otherwise it defaults to opening the current branch.
g-open() {
    git_branch=${2:-$(git symbolic-ref --quiet --short HEAD)}
    git_project_root=$(git config remote.origin.url | sed "s~git@\(.*\):\(.*\)~https://\1/\2~" | sed "s~\(.*\).git\$~\1~")
    git_directory=$(git rev-parse --show-prefix)
    open ${git_project_root}/tree/${git_branch}/${git_directory}${file}

A few potential improvements for the future:

  1. Combine and clean up the two sed commands.
  2. Support non-GitHub repositores.
  3. Allow the user to choose an alternative branch by using --branch instead of it being determined by parameter location.

Copy link

chevdor commented Feb 26, 2020

Because one more is likely a must :)

function gh() {(
    set -e
    git remote -v | grep push
    echo "Using remote $remote"

    URL=$(git config remote.$remote.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/")
    echo "Opening $URL..."
    open $URL

Tested on MacOS:

  • calling gh from a non git repo gives you an error message and exists
  • calling gh from a repo... opens it. Works with github, gitlab, etc...
  • calliing gh <remote> opens . For instance gh upstream.

Copy link

shinyaohtani commented Mar 19, 2020

I developed a small gem giturl to open github and github enterprise web pages for specified directories. Please try it.
(Sorry, I didn't know igrigorik/github.bash.)

  • usage

    $ giturl -o .
  • how giturl works
    I used these four git commands.

    $ git rev-parse --is-inside-work-tree
    $ git rev-parse --show-prefix
    $ git rev-parse --abbrev-ref HEAD
    $ git config --get remote.origin.url
  • installation

    $ gem install giturl
  • source code

Copy link

paulmicheli commented Sep 2, 2020

Just and FYI for Linux users

"open" is native on OS X, on linux you can just define it as alias open=xdg-open,

#Open GitHub from Repo
alias gh="xdg-open \`git remote -v | grep | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/http:\/\//'\`"

Copy link

this is a nice tool for doing this kinda stuff

Copy link

Copy link

JustinTRoss commented Dec 30, 2020

The alias commands above using print need the argument escaped. awk '{print $2}' should be awk '{print \$2}'.

Full command here:

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

Copy link

kuncevic commented Sep 2, 2021

This one works great for me

Copy link

eytanhanig commented Sep 30, 2021

I recently updated mine to work regardless of whether you are in the current repo. It supports both relative and absolute paths:


##  Open file or directory in web browser
##  Syntax: git-web $path [$branch]

if [ -d ${path} ]; then
  cd ${path}
  file_name=$(basename ${path})
  cd $(dirname ${path})
project_url=$(git config remote.origin.url | sed "s~:~/~" | sed "s~git@~https://~" | sed "s~\.git~~")
branch=${2:-$(git symbolic-ref --quiet --short HEAD)}
git_directory=$(git rev-parse --show-prefix)
echo ${project_url}/tree/${branch}/${git_directory}${file_name}
open ${project_url}/tree/${branch}/${git_directory}${file_name}

Copy link

With the new gh cli tool just use: gh repo view --web

Copy link

If using VS Code, one could also install the GitLens extension (good for other things too) and use the command palette (cmd + shift + P) > GitLens: Open Repository on Remote

Copy link

yohanb commented Jun 2, 2022

With the new gh cli tool just use: gh repo view --web

even easier, gh browse

Copy link

joeflack4 commented Jun 4, 2022

@drewbo worked for me on mac, thanks

Edit: Well, I don't actually want to use this, because when I source my .bash_profile now, I get this as a result:

fatal: not a git repository (or any of the parent directories): .git

Copy link

With Github CLI, this is exactly what I was looking:

gh browse .

Thanks @yohanb 🙏

Copy link

Quick and dirty if you already have the vim plugin rhubarb installed:

vim +GBrowse +q

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