Opening github in your browser from the terminal

Here is my first ever bash function! When you run it from the terminal it opens the github page in your browser for the git repository you are currently in. In keeping with bash best practices it has a short simple name: gh. I find myself doing this quite a lot as I prefer the github log and history views to the gitx or command line views.

# Opens the github page for the current git repository in your browser
# git@github.com:jasonneylon/dotfiles.git
# https://github.com/jasonneylon/dotfiles/
function gh() {
  giturl=$(git config --get remote.origin.url)
  if [ "$giturl" == "" ]
    then
     echo "Not a git repository or no remote.origin.url set"
     exit 1;
  fi

  giturl=${giturl/git\@github\.com\:/https://github.com/}
  giturl=${giturl/\.git/\/tree/}
  branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
  branch="(unnamed branch)"     # detached HEAD
  branch=${branch##refs/heads/}
  giturl=$giturl/tree/$branch
  open $giturl
}

Just type gh:
Opening github from the terminal

Boom! github!
The github page for the repository

How it works

It grabs the remote url via the git config command: git@github.com:jasonneylon/dotfiles.git. Next it does some simple string replacements to get a github http url: https://github.com/jasonneylon/dotfiles/. Finally it open the github url in the default browser using the MacOS open command. As it uses the open command I imagine it only works on MacOSs.

Advertisements

19 comments

  1. Andrew Nesbitt · April 22, 2011

    Have you seen hub?

    https://github.com/defunkt/hub

    It does this and so much more

    • jasonneylon · April 22, 2011

      That looks good. I did google for something like this but couldn’t find anything. They cheated by using ruby though. Real programmers use the bash!

  2. Eddie · August 12, 2011

    Lovely, thanks!

  3. Eric · August 22, 2011

    Awesome. I added in a bit that will take you to the current branch, if on a branch:

    • jasonneylon · August 24, 2011

      Thanks Eric!

      I’ve just updated my profile with your improved version.

      Jason

  4. danro · February 13, 2012

    Nice and simple. Thanks for posting đŸ™‚

  5. Chris · January 15, 2013

    FYI the Linux equivalent of “open” is “xdg-open”.

  6. Danny Tuppeny · April 22, 2013
  7. Dave Coleman · October 30, 2013

    Thanks for this cool function!

    I added the ability to specify which remote to open in the browser. This is useful if your project is forked and you have an upstream remote that you also manage. I also fixed an issue with “/tree” appearing twice.

    # Opens the github page for the current git repository in your browser.
    # Can pass in argument for which remote to use, defaults to ‘origin’
    function gh() {
    gitremote=”$1″
    if [ “$1” == “” ]; then
    gitremote=”origin”;
    fi

    giturl=$(git config –get remote.$gitremote.url)
    if [ “$giturl” == “” ]; then
    echo “Not a git repository or no remote.origin.url set”
    exit 1;
    fi

    giturl=${giturl/git\@github\.com\:/https://github.com/}
    giturl=${giturl/\.git/\/tree}
    branch=”$(git symbolic-ref HEAD 2>/dev/null)” ||
    branch=”(unnamed branch)” # detached HEAD
    branch=${branch##refs/heads/}
    giturl=$giturl/$branch

    open $giturl # mac
    xdg-open $giturl # linux
    }

  8. Maxime Lathuilière (@maxlath) · December 1, 2013

    hi, thanks for that!
    seems there’s a mistake in the current post version though:
    “giturl=$giturl/tree/$branch” produce something like https://github.com/user/repo/tree//tree/branch
    => “giturl=$giturl$branch” works just fine!

  9. Colm Morgan (@carmat71) · January 31, 2014

    I adapted this script to open your current repo/branch to view commits, branches, issues, pull requests using flags: https://gist.github.com/carmat/8728901

    • Jason Neylon · January 31, 2014

      Cool! thanks

      • Colm Morgan (@carmat71) · January 31, 2014

        No problem. It was n my to-do list for a while. Thanks for the original script, it’s a great help for productivity, yet simple

  10. Josmar Dias · June 17, 2014

    Nice script here man! It’s nice cause it relies only in bash (who doesn’t have it?).

    Node.js and JavaScript enthusiasts can count with this project https://github.com/zeke/ghwd
    Depends on Node.js and NPM, but, some people will have it anyway : )

  11. Bruno Batista · September 22, 2014

    Really nice feature, have any chance for this works in windows?

    • Jason Neylon · September 22, 2014

      I don’t use windows much any more so sadly not. I’m sure you could do something similar in Powershell.

  12. Daniel Gerep · March 25, 2015

    I have a shell project that does that:

    https://github.com/gerep/git-open

    Live long and prosper!

  13. Aamir Afridi · March 31, 2015

    Fixed the error in your code

    # Opens the github page for the current git repository in your browser
    # git@github.com:jasonneylon/dotfiles.git
    # https://github.com/jasonneylon/dotfiles/
    function gh() {
    giturl=$(git config –get remote.origin.url)
    if [ “$giturl” == “” ]
    then
    echo “Not a git repository or no remote.origin.url set”
    exit 1;
    fi

    giturl=${giturl/git\@github\.com\:/https://github.com/}
    giturl=${giturl/\.git/\/tree}
    branch=”$(git symbolic-ref HEAD 2>/dev/null)” ||
    branch=”(unnamed branch)” # detached HEAD
    branch=${branch##refs/heads/}
    giturl=$giturl/$branch
    open $giturl
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s