jperelli
jperelli

Reputation: 7197

Why does Git use SHA-1 as version numbers?

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?

Upvotes: 42

Views: 8061

Answers (3)

Holger Just
Holger Just

Reputation: 55718

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.

Upvotes: 39

Alex Wilson
Alex Wilson

Reputation: 6740

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.

Upvotes: 2

Robert Rouhani
Robert Rouhani

Reputation: 14668

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.

Upvotes: 9

Related Questions