I want to create a git alias which will allow me to do git visit and the repository will be opened in the browser.
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 anysed -e 's/ssh:\\/\\///g' removes ssh: part of the URL, if there is anysed -e 's/git@/https:\\/\\//g' then replace git@ with https:, when is presentsed -e 's/\\.git$//g' remove .git from end of the URLIf you are on a Linux-based system, replace the MacOS open command with xdg-open or start on Windows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With