I find myself frequently needing to go to the homepage of the projects I contribute to on GitHub. I have bookmarked the important ones, and a quick Google search of the project name and "github.com" should take me to the right place, but that can be time-consuming and requires a non-negligible amount of context switching. I decided to automate this process so as to not interrupt my workflow.
To do so, I wrote up a quick bash function to perform the automation, placed it in my .bashrc
file, and assigned it to the alias github
:
alias github=GitHub
function GitHub()
{
if [ ! -d .git ] ;
then echo "ERROR: This isnt a git directory" && return false;
fi
git_url=`git config --get remote.origin.url`
if [[ $git_url != https://github* ]] ;
then echo "ERROR: Remote origin is invalid" && return false;
fi
url=${git_url%.git}
open $url
}
Here's what that code is doing, in layman's terms:
- First, we check to see if we are at the root of a git repository by checking for the existence of a
.git/
directory. - Next, we get the
remote.origin.url
property from thegit config
. This is the URL of the remote git repo with which the project is associated. For GitHub projects, this takes the form:https://github.com/[USERNAME]/[PROJECT_NAME].git
- If the
remote.origin.url
is not from GitHub, we can't guarantee our ability to open it, and throw an error. - Finally, we remove
.git
from the URL and use theopen
command in macOS to open a browser at that given URL.
Now, any time I'm in the base directory of a project cloned from GitHub, I can open its project page straight from Terminal by running github
.
This is my first time really exploring the power of incorporating bash functions and aliases into my workflow. I'm now actively looking out for tasks I perform regularly that could be automated, and will try to create bash functions for them as needed.
This code snippet and another I've created to get the size of the working directory are on GitHub, and I plan to publish all the future ones I create as well! Feel free to contribute and share your own!
Some questions to inspire discussion:
What actions do developers perform regularly that could benefit from automation?
Have you used bash functions to automate your workflow? If so, feel free to share examples so others may learn! If not, could you see yourself using them in the future?
Top comments (19)
I use the ssh format for adding my remotes, this only seems to pick up http urls.
I hastily redid an else condition that will work with
git@github.com:
remotes as well.Just a small & quick modification to add current branch & non-github domain -
.../bin/git-browse
Nice!
Just for fun: do you have any idea how to add this as a
hub
command to git? ❤Just put it in a script called
git-hub
and add that to a directory in your path.Aye! Thanks!
Personally, I prefer this command to be associated with the
hub
alias, so I can just run:And since the folder name is really often the same as the repository name, here's my solution:
Make sure you change
<your username>
to your username.Now you can run
If the name of the folder doesn't correspond to the name of the repository, you can add a
.githubrepo
file and set its content to the name of it, and it'll use this instead.Hopefully it'll save some people a bit of time
There's a feature in Git called insteadOf that allows you to make shorthand URLs (so you can say things like
git clone me:my-project
rather thangit clone git@github.com:hoelzro/my-project
) -git-config
unfortunately doesn't handle insteadOf. If you want your function to work with these remote URLs, you can usegit ls-remote --get-url origin
instead.You might also want to use the
local
bash builtin to introducegit_url
andurl
as local variables to prevent your function from contaminating your bash session!Cool trick :). I have a bash alias
go='git browse'
that does the same using hub.github.com, but having the script on the dotfiles would be handy if I'm in a computer where its not available.Hi guys, this my own version for ssh and https, in one line, also you can run it in a subdirectory:
any tip on using two or more remotes (different credentials) on the same machine.. Let's say I use GitHub, my home NAS git and company's git server on the same machine for different projects... How to switch between these fast?
tips needed :) thanks in advance
You could just change your ssh config to use different identities based on the IP or something. If you need to use a different email, etc for certain projects you can use
git config user.email 'you@email.com'
(without--global
) to change it from the default global configuration.on Linux change
open
toxdg-open
or use
x-www-browser
in Ubuntuor use
sensible-browser
I've been using git-open and it has worked perfectly for me (even takes you to the current branch). Currently aliased to gh, though it also works with GitLab and Bitbucket.
npm install --global git-open
Bash scripts are great. Just wanted to point out there's a solid implementation of this though for those who want it.
I personally just use fugitive from VIM. If I ever need to see something from Github I just run
:Gbrowse
or if I want to just get the link to share it with someone I run:Gbrowse!
.Thanks for this. If you have GitHub Desktop installed - you may need to use a different alias.