How to open GitHub repository from the command line: gremote
One day I noticed myself opening Github repo for my project manually using type-in at browser. It happened often, sometimes I need to check the list of issues at GitHub or review fresh PRs. And it may happens several times a day because I’m a fan of closing non required in the moment browser tabs to reduce cognitive load.
There should be a way to open it right from console opened at the directory of your project.
The goal of the article to show how create bash script that runs from the git project directory and opens browser with URL pointing to the project remote repository. We’ll call it gremote
Let’s say you do you git remote -v
and see
git remote -vorigin <https://github.com/laravel/laravel.git> (fetch)
origin <https://github.com/laravel/laravel.git> (push)
Or you open another project with Git and see
git remote -vorigin git@github.com:laravel/jetstream.git (fetch)
origin git@github.com:laravel/jetstream.git (push)
As you see the URL of your remote may starts with https://
or git@
. It's time to extract and open remote repository for both cases and make bash functions to open it with one command. We'll name it gremote
.
Running gremote
from the first project folder will open https://github.com/laravel/laravel and from the second one will open https://github.com/laravel/jetstream
Case 1. Remote repository URL starts with https://
First let’s take a look how to open the remote repository at browser with remote starting with https
. Our goal from the list of remote URLs like origin <https://github.com/laravel/laravel.git> (fetch)
get the URL https://github.com/laravel/laravel that may be opened with browser.
Here is bash one-liner for that, run it into your project with remote https://
and you'll see your browser opening project URL. Don't worry we'll dive into it in a bit
How it works
- We get list of remote origin using
git remote -v
git remote -vorigin <https://github.com/laravel/laravel.git> (fetch)
origin <https://github.com/laravel/laravel.git> (push)
2. Leave only the first one using head -n 1
git remote -v \
| head -n 1origin <https://github.com/laravel/laravel.git> (fetch)
3. Split it into substrings by space and get second one using awk -F " " '{print $2}'
git remote -v \
| head -n 1 \
| awk -F " " '{print $2}'<https://github.com/laravel/laravel.git>
4. Remove .git
part using sed 's/\\.git//g'
git remote -v \
| head -n 1 \
| awk -F " " '{print $2}' \
| sed 's/\.git//g'<https://github.com/laravel/laravel>
5. And finally open it at browser using xargs open
git remote -v \
| head -n 1 \
| awk -F " " '{print $2}' \
| sed 's/\.git//g' \
| xargs open
Yep!🕺
Case 2. Remote repository URL starts with git@
Let’s move to more interesting remote URLs starting with git@
. Our goal from the list of remote URLs like origin git@github.com:laravel/jetstream.git (fetch)
get the URL https://github.com/laravel/jetstream
that may be opened with browser.
One-liner here is
How it works
- We get list of remote origin using
git remote -v
git remote -vorigin git@github.com:laravel/jetstream.git (fetch)
origin git@github.com:laravel/jetstream.git (push)
2. Leave only the first one using head -n 1
git remote -v \
| head -n 1origin git@github.com:laravel/jetstream.git (fetch)
3. Split it into substrings by @
and get second one using awk -F "@" '{print $2}'
git remote -v \
| head -n 1 \
| awk -F "@" '{print $2}'github.com:laravel/jetstream.git (fetch)
4. Now we need only github.com:laravel/jetstream.git
without (fetch)
part. We'll split github.com:laravel/jetstream.git (fetch)
into substrings by space and get first one using awk -F " " '{print $2}'
git remote -v \
| head -n 1 \
| awk -F "@" '{print $2}' \
| awk -F " " '{print $1}'github.com:laravel/jetstream.git
5. Let’s replace :
with /
to get github.com/laravel/jetstream.git
git remote -v \
| head -n 1 \
| awk -F "@" '{print $2}' \
| awk -F " " '{print $1}' \
| sed 's/:/\//g'github.com/laravel/jetstream.git
6. Remove .git
part with sed 's/\.git//g'
git remote -v \
| head -n 1 \
| awk -F "@" '{print $2}' \
| awk -F " " '{print $1}' \
| sed 's/:/\//g' \
| sed 's/\.git//g'github.com/laravel/jetstream
7. Add https:
to the start of string using awk '{print "https://"$1}'
git remote -v \
| head -n 1 \
| awk -F "@" '{print $2}' \
| awk -F " " '{print $1}' \
| sed 's/:/\//g' \
| sed 's/\.git//g' \
| awk '{print "https://"$1}'https://github.com/laravel/jetstream
8. And open it at browser using xargs open
git remote -v \
| head -n 1 \
| awk -F "@" '{print $2}' \
| awk -F " " '{print $1}' \
| sed 's/:/\//g' \
| sed 's/\.git//g' \
| awk '{print "https://"$1}' \
| xargs open
We did it!👋
It’s time to get all pieces together
To make it work as gremote
command let's write shell function and put it into bash startup file. I'll add it to ~/.bashrc
- At the script first I extract remote origins using
git remote -v | head -n 1
- Then I check if it has
https
with[[ $remote =~ 'https' ]]
- And choose correct script to build repo URL
- See how I use
echo $remote
to reuse results ofgit remote -v | head -n 1
through the variable - The last step is to print prompt for the user and open browser
Now you can open new shell or just do . ~/bashrc
at current shell and run gremote
at any project with git introduced.
bash-3.2$ pwd
/Users/mihan007/Projects/sandbox/laravel
bash-3.2$ gremote
Opening <https://github.com/laravel/laravel>
I’ve checked gremote
against GitHub, Bitbucket and GitLab - all works well. Hope you find it useful for your daily development needs. Also I hope that you found something new for you bash knowledge, it’s really powerful but a bit confusing swiss knife for true command line lovers like me.
Appendix 1. Adjust gremote to your needs
This is generic script to open main page of the repository. You may adjust it to open specific pages like issues or pull requests. To do that change the way how script build the url by modifying awk '{print "https://"$1}'
For example to open pull requests at GitHub use awk '{print "https://"$1"/pulls'}'
Doing that keep in mind that urls to specific part of repo are different for all services so modification for GitHub won’t work for Bitbucket or GitLab repos.
Appendix 2. For the fish shell users
Put this into ~/.config/fish/functions/gremote.fish