42

Git uses SHA-1 for the user to refer a commit.

Subversion (SVN) and Mercurial (hg) use an incremental number.

Why did the Git team make that design decision of using SHA-1 instead of something more descriptive?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jperelli
  • 6,988
  • 5
  • 50
  • 85

3 Answers3

39

Mercurial (hg) also uses SHA1 hashes. It just also tries to get revision numbers out of the commit history. However, these revisions are only valid in one repository. If you watch another repo, these revisions are not guaranteed to match.

As to why git and mercurial use hashes: both have a non-linear commit history. because both are distributed, people can work on the same code basis in their local repositories without having to synchronize to a central authority (as required by SVN and CVS). Now if people commit their stuff locally and merge them later, you will have a hard time to come up with a consistent schema to form linearly increasing integer revisions. And even if you could, you would still get different result between different repos.

In the end, it's all because of the distributed nature. It is a simple way to come up with rather unique identifiers to commits. And as a side-product you can also encode the complete history towards a single commit into the hash. Which means, even if you have the same diff in a commit, you probably will get different SHA1 hashes.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Holger Just
  • 52,918
  • 14
  • 115
  • 123
9

It's used both as a method of commit identification and of verification. The SHA1 number is the checksum of all the changes made in the commit plus the name of the author plus the SHA1 of the parent commit. (and IIRC it also includes the commit message, not sure though).

When you pull/fetch from a remote, git will check the checksum against the files that you received to make sure that you didn't receive a corrupted or modified version of the code.

Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • 8
    Just to be precise, the checksum isn't of the *changes* made in the commit, it's of the whole tree, and yes, it includes the commit message. Every tree is referred to by a SHA1 name as well, as is every file in that tree. – Dan Hulme Jun 27 '12 at 19:35
  • @robert-rouhani the way I understand it in a simple way, it helps team-mates to have the same commit identifiers on the main repo as the ones git dedicated to their commits on their local machines, am I right? – aderchox Nov 19 '18 at 18:59
2

Git and HG both use SHA1 to identify commits. SVN operates a sequential model of change and so is able to support an incremental version number, whereas Git and HG are much more sophisticated and support a directed acyclic graph model of development which is much more amenable to frequent branching and merging.

The HG incremental number is different from the SVN incremental number and is not suitable for refering to a particular revision across different copies of a repository, so cannot be used in the same way as the SVN revision number.

Alex Wilson
  • 6,690
  • 27
  • 44