Why must we use the first seven characters of a SHA hash to identify a commit?


#1

Question

We learn here that when using git reset with the commit’s SHA hash, we should use the first seven characters of the hash. Why is this?

Answer

The reason for this is to reduce the potential ambiguity of the commit you are selecting. The characters are hexadecimal digits (0-9, a-f) and so by using seven characters, there are over 250 million potential hashes. This range reduces the possibility of collision.

Note that we can use a smaller number of characters to identify a commit at the cost of a higher chance of collisions. If ever the hash characters you choose are ambiguous, you will receive a error message stating so.


FAQ: How to Backtrack - git reset II