Focus! Never click around Github or Gitlab again

1. Introduction
So you are working in your favorite editor, using shortcuts and automation as much as you can, andcomes the time to use Github or Gitlab to check your CI, update some issues or PRs and here you go again:
-
open your browser
-
search for your bookmark(s) for this project
-
where is that bookmark again ?
-
oh well, maybe I did not make one
-
search for the project
-
oh that is the original project, not my fork
-
your email tab opens
-
hey new email, what is it about ?
-
so where was I ?
…I am sure you recognize some of these steps from your daily routine. Not only this is taking some time but it also pulls you away from your main task and you lose focus.
The time has come to improve that and we need less than 5 minutes…
2. Bash magic
The following snippet defines a bash function called gh
. Despite its name, it works for Github as well as Gitlab and maybe other git based solutions although I did not test.
gh ()
{
( set -e;
git remote -v | grep push;
remote=${1:-origin};
echo "Using remote $remote";
URL=$(git config remote.$remote.url | sed "s/git@\(.*\):\(.*\).git/https:\/\/\1\/\2/");
echo "Opening $URL...";
open $URL )
}
You typically should add this code in your user’s home folder in: ~/.bash_profile
.
Once you added the snippet and saved the file, run source ~/.bash_profile
to ensure the changes are loaded in your current shell. That’s it, you are ready to save time!
3. Usage
3.1. Basic
First of all, this scripts assumes that you have git
installed on your machine… If you read so far, I bet you do!
When you work on your favorite project, from a shell, issue the following command: gh
.
⇒ as a result, your favorite browser will open either Gitlab/http://www.github.com[Github, window=github] in the project page matching your repo. That was easy!
3.2. Multiple remotes
You will often work with several remotes, you can see a list with:
git remote -v
Typically, you will see (your usage may vary):
-
origin
: that could be your fork for instance -
upstream
: that could be the original project you cloned for instance
Note
|
I personally prefer using Gitlab but the main projects are often still on Github so it is not uncommon for me to have a remote on Github but my main repo on Gitlab. Your actual names and list may vary depending on your strategy. |
You may append the <name>
of your remote to open it directly:
gh origin
⇒ will open the project page of your origin
remote (on Gitlab for instance).
gh upstream
⇒ will open the project page of your upstream
remote (on Github for instance)
4. Share the bash love
If you find this convenient and already started using it, make sure to share this tip further with friends and colleagues with the sharing buttons below.
Focus! Never click around Github or Gitlab again.
— Chevdor (@chevdor) July 25, 2020
Here is an easy tip to open the 'right' repo page, directly from your shell.https://t.co/qaZ4Pa819H #dev #Productivity #bashTricks #Git @github @gitlab