12

I'm using git on a Mac, and I'd like to know if there's a command to open the remote repository (origin) in a browser from the terminal.

Karan
  • 55,947
  • 20
  • 119
  • 191
Chris
  • 433
  • 1
  • 4
  • 12
  • "Open" certainly isn't a git technical term... – Ignacio Vazquez-Abrams Feb 06 '13 at 07:54
  • Right. I added the "in a browser" to clarify – Chris Feb 06 '13 at 08:00
  • For example, the equivalent with Heroku would be `heroku open` – Chris Feb 06 '13 at 08:03
  • The remote repository is the URL with `.git`, e.g. `git@github.com:slhck/foo.git`, so how'd you map this to whatever HTML representation there is? Are you talking about a special repository like GitHub only, or… ? – slhck Feb 06 '13 at 08:14
  • 1
    At least with Github, the URLs that come up when I run `git remote -v` seem to be accessible when plugged into a browser. I guess this wouldn't apply in all situations, though. – Chris Feb 06 '13 at 08:37
  • As far as dirty hacks go, I found that the following works when proper URLs come up with `git remote -v`:`$ git remote -v | awk '/origin.*push/ {print $2}' | xargs open` – Chris Feb 06 '13 at 08:39
  • @chrisf, "opening the git repo in the browser" works _only_ if the remote repository is set up to be accessed by HTTP, for example by http://gitorious.org or by the (quite limited) HTTP server that comes bundled with git itself. – vonbrand Feb 06 '13 at 19:10
  • @vonbrand I was mainly referring to remote repo services like Github or Bitbucket that have web-accessible pages. – Chris Feb 07 '13 at 01:52

6 Answers6

7

there's a github project for your wish

Robert
  • 171
  • 1
  • 2
5

It's kind of ugly, and will only work in a few cases, but I came up with a way that works for me.

$ git remote -v | awk '/origin.*push/ {print $2}' | xargs open

I then assigned that to the alias gitrm. I'm not sure if open works on anything besides OSX, though.

In the end I realized that not every remote repository has a friendly web-based frontend, so it wouldn't really make sense for git to provide a command to open them.

Chris
  • 433
  • 1
  • 4
  • 12
  • 1
    For those of you who are slow to remember about escaping your alias commands, let me remind you to escape the $: `alias gitrm="git remote -v | awk '/origin.*push/ {print \$2}' | xargs open"` – Michael Paulukonis Jul 18 '18 at 19:55
  • ha, this is exactly what happened to me when I made this alias, thanks Michael – davidawad Dec 30 '22 at 18:37
3

If your remote output is something like this:

origin git@github.abc.xyz.com:opp/wee.git (fetch)

git remote -v | head -n 1 | awk -F "@" '{print $2}' | awk -F " " '{print $1}' | sed 's/:/\//g' | sed 's/.git//g' | awk '{print "http://"$1}' | xargs open

will open http://github.abc.xyz.com/opp/wee in the browser. I haven't come across other remote outputs, so hopefully this works mostly.

Siddhartha
  • 817
  • 2
  • 6
  • 18
  • this is the only one that works with arbitrary ssh remotes. I'd update `http` to `https` though – kdavh Oct 26 '22 at 21:06
3

I made this little bash function for what you need.

function gbrowse {
    gbrowsevar=$(git config --get remote.origin.url)
    printf "${gbrowsevar}"
    start $gbrowsevar
}

Add it to your local file .bashrc, usually at the home folder in any OS. Then use it from the bash command line just by typing in gbrowse.

The third line inside the function will open the remote repository using the default browser. If you want a custom one, do instead something like start firefox $gbrowsevar

By the way, you need to be in your local repo (at any folder level below works the same, as far as I tested it).

Usage example (it will open in the last window you used):

enter image description here

By the way, that repository I used in the example is public, and you can find some files I find useful, including a copy of my .bashrc file (worspaces-and-settings folder), with some useful git commands in it (I'm still working in it though).

Bonus

For Windows systems.

# GIT: open remote repository using Google Chrome
function gbrowse {
    NC='\033[0m' # No Color
    SB='\033[1;34m' # Sky Blue
    PP='\033[1;35m' # Purple
    gbrowsevar=$(git config --get remote.origin.url)
    printf "\n${PP}→ ${SB}gbrowse:${NC} Chrome\n"
    printf "${gbrowsevar}\n"
    start chrome $gbrowsevar
}

# GIT: open remote repository using Firefox
function fbrowse {
    NC='\033[0m' # No Color
    SB='\033[1;34m' # Sky Blue
    PP='\033[1;35m' # Purple
    fbrowsevar=$(git config --get remote.origin.url)
    printf "\n${PP}→ ${SB}fbrowse:${NC} Firefox\n"
    printf "${fbrowsevar}\n"
    start firefox $fbrowsevar
}

How do you change the color scheme in bash on Ubuntu for Windows?

carloswm85
  • 141
  • 4
1

You can"t view the remote repository in a browser as the browser requires a webserver. What you can do though is clone the remote repository (if you haven't done so already) and then run the git instaweb command in the directory of the local repository, which will let you browse the history, branches, commits, diffs...

eikes
  • 175
  • 2
  • 4
  • 2
    I like that. Not quite what I was talking about, but perhaps this is a preferable alternative. – Chris Feb 08 '13 at 14:19
0

I wrote a cli (to-where-cli) that can help you solve this problem, just execute:

npm install -g to-where-cli
// open current remote branch
tw git open 
// open master/main remote branch
tw git open -m

github: https://github.com/skypesky/to-where-cli

npm: https://www.npmjs.com/package/to-where-cli