Where are git commit IDs stored?

Having a git repository I'm curious where the git commit IDs are stored and how they are stored there.

Is it possible to force the git commit id to be modified to something special like hello-world

(or maybe have 40 characters)?

+3


source to share


3 answers


Git has a data store called an object database. Commits are also objects for git and are stored in this object database. See the Git internal documentation for details .

Every object that is stored in the object database is stored using a SHA-1 hash. For example.

echo 'test content' | git hash-object -w --stdin
d670460b4b4aece5915caf5c68d12f560a9fe3e4

      

Since a commit is just an object, it also receives a SHA-1 hash. You can view the contents of the commit object with git cat-file

. Here is an example from the apas commons-io repository.

git cat-file -p 35f306967d0641e7d49cafb25938a4f69a36e77a
tree 289b9d5a11b7f52d330e86a30fd1c7d138703f4b
parent c57af0ad5604e8280884f6183e05eb30751883ef
author Sebastian Bazley <sebb@apache.org> 1431094588 +0000
committer Sebastian Bazley <sebb@apache.org> 1431094588 +0000

Each Version must be in its own release section

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1678358 13f79535-47bb-0310-9956-ffa450edef68

      



The end is just a text file that you can see. It contains

  • wood
  • parental
  • Author
  • committer
  • commit message

Thus, the commit ID changes only if at least one of these attributes changes.

Is it possible to force the git commit id to be forced into something special like hello-world

Not. The commit ID is the SHA-1 hash of the object's content, so it only changes when the object's content changes. git uses SHA-1 to compare objects for equality. For example. if you do git fetch

, git can easily detect if an object is already present in your local object database.

+1


source


Git stores the ID in the database.



You must not change your name. If you want to name the commit, instead of Tags .

+3


source


So let's say you are on a branch develop

. Then there will be a file named .git/refs/heads/develop

. This will store the SHA hash on the commit it points to. This, in every sense and purpose, is the entry point of all operations. Everything else that is done for this branch usually refers to this SHA.

The entire git database is stored in a directory .git/objects

. You can find the commit there. Just strip the first two letters of the SHA commit and find the directory with that name. For example, if your commit 5f352ad1adcaf5a4bfc638d53f13db62c23d34e9

, the file might be called .git/objects/5f/352ad1adcaf5a4bfc638d53f13db62c23d34e9

. But git does a lot of things to save space, so it couldn't be named if your git repository was packaged. Peeping inside the file will show compressed unreadable data.

Obviously, you cannot rename this file to something else and expect it to work. Besides naming, SHA is also used to check the integrity of a file. So a random string won't do.

+2


source







All Articles