Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a git command to open repository in browser

Tags:

git

git-alias

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

like image 787
Jonathan1609 Avatar asked Oct 24 '25 11:10

Jonathan1609


1 Answers

I have the following alias in the .gitconfig file:

[alias]
open = "!f() { open $(git config remote.${1:-origin}.url | sed -E 's/:([^\\/])/\\/\\1/g' | sed -e 's/ssh:\\/\\///g' | sed -e 's/git@/https:\\/\\//g' | sed -e 's/\\.git$//g'); }; f"

This command opens the URL with your default browser, just run git open or git open <remote>. It's based on following command, which returns the remote URL of the local repository:

git config remote.origin.url

But this URL can be either git@... or https:// URL. Result may look like this:

[email protected]:OzzyCzech/dotfiles.git

or

https://github.com/OzzyCzech/dotfiles.git

To make it easy to open, the address needs to be modified to valid URL with a few sed commands:

  • sed -E 's/:([^\\/])/\\/\\1/g' replace : part inside URL with /, if there is any
  • sed -e 's/ssh:\\/\\///g' removes ssh: part of the URL, if there is any
  • sed -e 's/git@/https:\\/\\//g' then replace git@ with https:, when is present
  • lastly sed -e 's/\\.git$//g' remove .git from end of the URL

If you are on a Linux-based system, replace the MacOS open command with xdg-open or start on Windows.

like image 183
OzzyCzech Avatar answered Oct 26 '25 04:10

OzzyCzech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!