I would like to get the SHA number from from GIT log in abbreviated format. This command will work:
git log -1 --format=%h
However, the default abbreviated format is 7 numbers in length. Is there any way to change that?
A commit in git always has a hash that contains 40 characters. But to make the id:s easier to handle it also supports using a short version of the id. The short commit id can actually be any number of characters as long as it's unique for a commit within the same repo.
Git Log OnelineThe oneline option is used to display the output as one commit per line. It also shows the output in brief like the first seven characters of the commit SHA and the commit message. It will be used as follows: $ git log --oneline.
1.1. Command: git log --pretty=raw. The raw log output format shows the entire commit exactly as stored in the commit object. commit <sha-1> tree <tree-sha-1> parent <sha-1 of the previous commit object>
short-sha is a GitHub Action than provides an output sha with the shortened commit SHA. ⚠️ On November 16th, 2020 Github removes the set-env command. Version prior to v1. 2 of this action will not work. See github annoucement for details.
For git log
, the --abbrev=<length>
parameter controls how long the output for %h
and other abbreviated hashes is:
$ git log -1 --format=%h --abbrev=4
d157
I will also note that when using -1
(or --no-walk
which has the same effect in this particular case, but is more useful if you specify several commit identifiers), if all you want is the commit hash, using git log
is overkill: git rev-parse
will get you the hash. For no obvious reason, the control knob for limiting git rev-parse
's commit IDs to a particular length is spelled --short
rather than --abbrev
; and git rev-parse
requires that you spell out HEAD
if you mean HEAD
, so:
$ git rev-parse --short=4 HEAD
d157
The longest is pretty long, currently 40 characters, probably 64 in the future. The shortest you can ever go is four characters, which works in tiny repositories. But the shortest you can go in some particular repository may be more than four characters.
For output, you can ask for the --short
or --abbrev
length to be any value you want. Values that are too small or too big will be raised or lowered as needed. (Note that in truly ancient versions of Git, it might show you four character hashes if you ask for them, even if they're too short to be unambiguous. Current Git is smarter.)
When you supply a shortened raw hash ID of at least four characters yourself, though, if it's too short, you'll get an error like this one:
$ git rev-parse 1111
error: short SHA1 1111 is ambiguous
hint: The candidates are:
hint: 111116ea13 blob
hint: 1111f64dd9 blob
1111
fatal: ambiguous argument '1111': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Older versions of Git aren't as nice about their error messages; this one, which—if you read the hint:
output lines—tells you that you need at least 11111
or 1111f
to pick one of the possible results, is from Git 2.27.0.
Since Git repositories grow over time, it's possible to use a very short hash ID early in the repository's lifetime, and later—say, in five years—discover that this short hash ID is now ambiguous. The Linux kernel, for instance, is now up to the point where git log --oneline
uses 12 characters for safety. If you set a very short --abbrev
, the git log
output will have varying output hash sizes since each one gets extended to the necessary minimum:
$ git log --oneline -n 12 --abbrev=4
0f1a7b (HEAD -> master) timer-of: don't use conditional expression with mixed 'void' types
5021b9 Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
714366 Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
65aa35 Merge tag 'erofs-for-5.4-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs
3fd57e7 char/random: Add a newline at the end of the file
Note how commit 0f1a7b3fac0583083ca19d4de47403511ced3521
was able to be shortened to 0f1a7b
(six characters), but commit 3fd57e7a9e66b9a8bcbf0560ff09e84d0b8de1bd
took seven (3fd57e7
). There are currently two objects with 3fd57e
as their first six hexadecimal digits of their hash IDs: one commit object and one tree object. Over time, as more objects accumulate in the Linux kernel repository, even 3fd57e7
may become ambiguous.
You can get the full hash with :
git log -1 --format=%H
You can also use an arbitrary number of chars with, for example for 6 digits:
git log -1 --format=%h --abbrev=6
To try how much saturated is the hash of the repo do the next:
git rev-list --all --abbrev=0 --abbrev-commit |
awk '{ a[length] += 1 } END { for (len in a) print len, a[len] }'
I hope this helps :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With