Copy the last git commit hash on macOS

When responding to code-review requests, I like to point the reviewer to the specific commit where the issue was addressed with a message like:

Fixed in 4908c91a06258814077b1358210eb78cc718609c

GitHub will automatically detect the hash, shorten it, and convert it into a link to the commit view.

Rather than going into git log and copying the hash manually, I use the following one-liner in the terminal on macOS:

git rev-parse HEAD | pbcopy

The first half of the command returns the last commit hash from HEAD, which is a reference to the current branch. You could use git rev-parse name_of_branch too, but that is often longer to type.

The output of git rev-parse is then piped to pbcopy, the clipboard utility for macOS. If you were using a different platform like Unix or Windows, there are other options for you.

To save some more time, I added an alias my shell’s rc file:

# git last commit (hash)
alias glc="git rev-parse HEAD | pbcopy"

Happy copying!

Hebrews 8:5 (SDG)


My name is Sean McPherson (SeanMcP), and I am a software engineer based in Pittsburgh, PA. Here I write articles about programming for developers of all levels.