Git Alias to browse

git 11 comments suggest edit

Happy New Year! I hope you make the most of this year. To help you out, I have a tiny little Git alias that might save you a few seconds here and there.

When I’m working with Git on the command line, I often want to navigate to the repository on GitHub. So I open my browser and type in the URL like a Neanderthal. Yes, a little known fact about Neanderthals is that they were such hipsters they were using browsers before computers were even invented. Look it up.

But I digress. Typing in all those characters is a lot of work and I’m lazy and I like to automate all the things. So I wrote the following Git alias.

[alias]
  open = "!f() { REPO_URL=$(git config remote.origin.url); explorer ${REPO_URL%%.git}; }; f"
  browse = !git open

So when I’m in a repository directory on the command line, I can just type git open and it’ll launch my default browser to the URL specified by the remote origin. In my case, this is typically a GitHub repository, but this’ll work for other hosts.

The second line in that snippet is an alias for the alias. I wrote that because I just know I’m going to forget one day and type git browse instead of git open. So future me, you’re welcome.

This alias makes a couple of assumptions.

  1. You’re running Windows
  2. You use https for your remote origin.

In the first case, if you’re running a Mac, you probably want to use open instead of explorer. For Linux, I have no idea, but I assume the same will work.

In the second case, if you’re not using https, I can’t help you. You might try this approach instead.

Update 2017-05-09 I updated the alias to truncate .git at the end.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

11 responses

  1. Avatar for Aidan Feldman
    Aidan Feldman January 4th, 2017

    Nice! https://hub.github.com/ also provides a 'git browse' command.

  2. Avatar for Leonardo Taglialegne
    Leonardo Taglialegne January 4th, 2017

    xdg-open works on a lot of Linuces

  3. Avatar for Phil Cleveland
    Phil Cleveland January 4th, 2017

    Very cool. Thanks. I use Brave on Linux and it works if I replace explorer with brave.

  4. Avatar for olsh
    olsh January 4th, 2017

    There is another approach.
    https://github.com/pauliris...

  5. Avatar for Andrew Flierman
    Andrew Flierman January 5th, 2017

    For KilnHg repo's it doesn't work. It appends .git to the url and apparently Kilnhg doesn't like that

  6. Avatar for Laurent Kempé
    Laurent Kempé January 5th, 2017

    Doesn't work for me on Windows 10 because I get the url starting with https://laurentkempe@github... and the username seems to confuse explorer which opens a folder and not my default web browser!

  7. Avatar for haacked
    haacked January 5th, 2017

    Ah, I need to cut the trailing ".git"

  8. Avatar for Aidan Feldman
    Aidan Feldman January 5th, 2017

    Pretty sure GitHub redirects it, so not the end of the world, depending on the browser maybe?

  9. Avatar for Andrew Flierman
    Andrew Flierman January 6th, 2017

    GitHub does, but KilnHg not ;-)

  10. Avatar for Jakub Jankiewicz
    Jakub Jankiewicz October 8th, 2017

    it don't work for git@ urls for those you can use this:

    open = "!f() { URL=$(git config remote.origin.url | sed -e 's%^git@[^:]\\+:%https://github.com/%' -e 's/.git$//'); xdg-open $URL; }; f"

  11. Avatar for Peter M
    Peter M October 17th, 2017

    I'm little late to the party but this my take on this.
    First, you assume that everybody uses http(s) when cloning a repo but that's not always the case :-). Anyway, my alias looks like this:


    # open repo in browser, usage: git open [upstream]
    open = "!f() { URL=$(git config remote.${1-origin}.url | sed -e 's#^.*@#https://#' -e 's#.git$##' -e 's#:#/#2'); git web--browse $URL; }; f"


    It will handle both http(s) and git cloned repositories.
    Uses git web--browse, the git helper script to launch a web browser, platform independent.
    Pass the remote name as an optional parameter e.g. upstream , default is origin.