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.
8 Answers
There's a github project allowing you to open git repository in your browser with git open command. It supports various remote repositories (not only GitHub, but also Gitlab, Bitbucket and others) and works also in cases where git remote -v returns URL in a git@... format.
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.
- 58,727
- 857
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):
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?
- 171
- 1
- 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.
- 453
- 1
- 4
- 12
Quick alias to be placed in .zshrc/.bashrc.
alias remote='open "$(git remote get-url origin | sed "s/\.git$//")"'
Note: open is designed for files/dirs/URLs on MacOS. Linux users may need to substitute with xdg-open.
- 111
- 4
You can’t view the remote repository in a browser as the browser requires a web server.
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.
- 58,727
- 160
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
Alias for macOS Terminal, for ssh URLs:
alias gitro="open $(git remote get-url origin | sed 's|git@github.com:|https://github.com/|')"
For https URLs:
alias gitro='open $(git remote get-url origin)'
Put in .profile or similar. Works on Sequoia.
- 111
