Download Pragmatic Version Control

Transcript
M AKING C HANGES
Joe Asks. . .
What Are SHA-1 Hashes, and Why Does Git Use Them?
If you’re working with a centralized VCS where one server has
the authority to designate one number to each commit, numbers make sense. If you’re working in a distributed environment,
they don’t.
When two different people are working on the same code,
who’s to say what revision 181 is between the two of them?
Git addresses this by using SHA-1 hashes.
SHA stands for Secure Hash Algorithm. It’s an algorithm developed by the U.S. National Security Agency (NSA) to produce
shorter strings, or message digests, of known data with little possibility of a “collision” with another hash.
Git generates commit names by using some metadata from
the repository, your information as the person making the commit, and the current timestamp. This data combined produces
a unique hash that has a small probability of ever colliding with
another commit.
The possibility does exist, though, but it’s low enough we can
safely ignore it. For those math majors out there, or those who
like trying to pronounce large numbers, the chances are 1 in
263 , or 1 in 9,223,372,036,854,775,808. In American English, that’s
nine quintillion, two hundred twenty-three quadrillion, three
hundred seventy-two trillion, thirty-six billion, eight hundred fiftyfour million, seven hundred seventy-five thousand, and eight
hundred and eight. And, yes, I did have to look that up.
These forty-character hashes are kind of hard to remember,
though, and many times they’re unnecessarily long. Most of
the time the first seven or eight characters of a hash will be
unique enough to match. Throughout Git, and this book, you’ll
see shorter commit hashes used. When Git uses smaller hashes,
it uses the first seven characters.
The second line in the git log output is the commit’s author info, the
third is the date the commit was made, and the final bit of information
is the commit log message from the commit.
Report erratum
Prepared exclusively for Frode Nilsen
this copy is (P2.0 printing, March 2009)
39