The | head
method is fine—the head
program is a general purpose filter for extracting the front part of an input stream, or some number of input files—but it's worth noting that the first line of the default git log
output consists of the word commit
followed by the commit's hash, which (perhaps not coincidentally) is spelled out as 40 characters:
$ git log | head -n 1
commit 8f60064c1f538f06e1c579cbd9840b86b10bcd3d
Since commit
(including the trailing space) is 8 characters long, if you chop this down to 40 characters, you get a 32-character abbreviation of the commit ID.
Since git log
normally starts by showing you the HEAD
commit, this all means that you are getting (part of) the hash ID of the HEAD
commit, and there is a much more direct way to do that in Git:
$ git rev-parse HEAD
8f60064c1f538f06e1c579cbd9840b86b10bcd3d
This omits the word commit
(and the space) but gets you the 40 characters that I suspect you care about. You can shorten the hash to any number of characters you like by adding --short
or --short=count
:
$ git rev-parse --short=12 HEAD
8f60064c1f53
In general, the way to turn a single name—such as master
, or a tag name, or HEAD
—into a Git object identifier (SHA-1 hash) is to use git rev-parse
.
head
. – Feticide