pátek 12. prosince 2014

Git error: Short SHA1 is Ambiguous

Git allows using shortened SHA1 hashes. Default is to use 7 characters (see core.abbrev config option). But sooner or later, you may hit problem, that short hashes are ambiguous. E.g.
$ git log 0ed98e5
error: short SHA1 0ed98e5 is ambiguous

To find, what Git objects are referenced by the same short has, use
$git rev-parse --disambiguate=0ed98e | git cat-file --batch-check
0ed98e56dd43b172d438dba0aa6ea9ebed0554c7 commit 307
0ed98e50771521f9ec27314d49286fd54e989c87 blob 1478
0ed98e50771521f9ec27314d49286fd54e989c87 blob 1478

In this case, only the first hash (0ed98e56) refers to a commit. The second hash (0ed98e50) refers to a blob - file. So, Git does check SHA1 for all it's objects, not only for commits, although the git log command uses just commits SHA1 for it's arguments.

How to fix the error? Use longer prefix of the SHA1 hash. The bulletproof solution is to use all 40 characters. But if you still like abbreviates, then you can find the safe minimum length of SHA1 prefix for your project by Josh Stone's solution:
$git rev-list --all --abbrev=0 --abbrev-commit | wc -L