Getting full Github URL of the latest commit

I want to share my latest commit’s url, previously I would go to Github repo, click the latest commit message, then copy the url.

gh repo view -w # Opens current repo in the browser

It turns out that building the URL is much simpler

gh browse -c -n | pbcopy # Returns current url of the last commit tree, then pipe to macOS clipboard
# e.g.

By the way I want to see the commit diff not the file tree, naive approach

gh browse -c -n | sed "s/\/tree\//\/commit\//" # Replaces /tree/ with /commit/

Neat! But it will cause bad URL if you ever have a repo called tree

So that I asked ChatGPT but…

I fallback to good ol’ StackOverflow and found the ultimate solution

echo "$(gh repo view --json url --jq .url)/commit/$(git rev-parse HEAD)"