Last Updated: September 27, 2021
·
1.097K
· mdahlstrand

git permalink

Sometimes you're sat in terminal and you want to open your last commit in the browser. By adding the following line to the alias section of your gitconfig file (normally at ~/.gitconfig), opening the last commit's github page is as easy as typing git permalink:

permalink = "!f() { echo "https://$(git config --get remote.origin.url | grep --color=never -o -E 'github.com[:/][^\\.]+' | sed s/\\:/\\\\//)/commit/$(git rev-parse @{u})"; }; open $(f)"

One interesting thing to notice is that by using [:/] in the grep pattern I match both https and ssh github origins, and with sed I replace the possible colon with a slash to make sure it works as an url.

Also, by using git rev-parse @{u} we make sure that we only get hashes for commits that are available on github.

Gist with a bit-by-bit breakdown of what this oneliner does