1

I want to create a git alias which will allow me to do git visit and the repository will be opened in the browser.

Jonathan1609
  • 1,809
  • 1
  • 5
  • 22

7 Answers7

4

When you type a command such as git xyz, git looks for a file named git-xyz and if found, it executes it. You can simulate such behavior by putting a file named git-visit in a directory which is in the PATH, and then git visit will execute it.

The content of the file should be

xdg-open $(git remote -v | cut -d @ -f 2 | cut -d ' ' -f 1 | head -1 | sed 's/:/\//' | sed 's/.git$//' | sed s'/^/https:\/\//') >& /dev/null &

(xdg-open might not work on your machine).

Note that you must do chmod +x git-xyz in order to make the file executable

Jonathan1609
  • 1,809
  • 1
  • 5
  • 22
4

This works for me in Git for Windows with Git Bash. Place the following in your git configuration file:

[alias]
visit      = "!git remote get-url origin | xargs -r start"
visitfancy = "!f() { REMOTE=${1:-origin}; URL=$(git remote get-url \"$REMOTE\"); if [[ -n \"$URL\" ]]; then start \"$URL\"; fi; }; f"

visit will just launch the remote url of origin if it exists.

visitfancy will launch the remote url of the remote name you specify, or default to origin if none is specified.

kwill
  • 3,211
  • 1
  • 16
  • 18
1

If this repo has a GitHub remote, you can say hub browse (if you have installed hub).

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I am using the following:

[alias]
        visit = "!f(){ xdg-open `git config --get remote.origin.url | sed -Ee 's#(git@|git://)#https://#' -e 's@com:@com/@'`| head -n1; }; f"

It works for SSH and HTTPS cloned repository + GitHub and GitLab

0

what I use on osx:

Use the desired browser by simply replacing Firefox.app with eg. Safari.app, Brave Browser.app, Google Chrome.app ...

alias visit='open -a "/Applications/Firefox.app" $(git config --get remote.origin.url)'
Joe Web
  • 558
  • 5
  • 11
0

On windows you can run the following command in your repo terminal:

start (git config --get remote.origin.url)
0

There is a tiny and handy tool for this purpose. You can easy install it via npm and different commands to open your repo in the browser.

- npm install --global git-open
- git open

Here you can find the documentation for other useful commands

pouyada
  • 341
  • 3
  • 15