Git: Show and copy commit SHAs

You can use git rev-parse to show the SHA of the current Git commit:
$ git rev-parse @
44a4ec1ffe634759fd25a9e87a6c555fe83f7d64
@ is short for HEAD, Git’s name for the current commit.
Use --short for the abbreviated SHA:
$ git rev-parse --short @
44a4ec1
From a branch or tag
rev-parse supports many other kinds of reference.
To show the SHA of a branch or tag, use its name:
$ git rev-parse main
44a4ec1ffe634759fd25a9e87a6c555fe83f7d64
Remote branch and tag references also work:
$ git rev-parse origin/main
385bb2ff3d2ce74690a1583eefa216ed61e8d28a
Copy to clipboard
If you need a commit SHA, you probably want to use it elsewhere, so copy-pasting it could be helpful. When copying, you probably want to drop the trailing newline from the output, which you can do with tr. Then, use your system’s copy CLI command to put the SHA into your clipboard. For example, on macOS, use pbcopy:
$ git rev-parse @ | tr -d '\n' | pbcopy
Refer to my “copy to clipboard” post for the commands on other platforms.
😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸
One summary email a week, no spam, I pinky promise.
Related posts:
Tags: git