Does Git warn me if a shorthand commit ID can refer to 2 different commits?

It should give you something like this:

$ git log cee157
error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.
fatal: ambiguous argument 'cee157': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I just tested this on a real Git repository, by finding commits with duplicate prefixes like this:

git rev-list master | cut -c-4 | sort | uniq -c | sort -nr | head

This takes the list of revisions in master, cuts out the first 4 characters and throws away the rest, count the duplicates and sort numerically. In a my relatively small repository of ~1500 commits I found quite a few revisions with a common 4-digit prefix. I chose a 4-digit prefix because that seems to be the shortest legal length supported by Git. (Doesn’t work with 3 digits or less, even if not ambiguous.)

Btw this was not a typo, I don’t know why the error message about ambiguous SHA1 appears twice, regardless of the number of duplicate SHA1 (tried with 2 and 3):

error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.

(Both on stderr. Actually the entire output is on stderr, nothing on stdout.)

Tested in Windows:

$ git --version
git version 1.8.1.msysgit.1

I think it’s safe to say that if your version is >= 1.8.1, Git will warn you of duplicates. (It will refuse to operate with duplicates.) I would guess that much older versions worked this way too.

UPDATE

When testing this, you need a minimum of 4-digit SHA1, because of int minimum_abbrev = 4 in environment.c. (Thanks @devnull for pointing that out!)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)