Reputation: 113355
Having a git repository I'm curious where are the git commit ids stored and how they are stored there.
Is it possible to force modifying a git commit id into something special, for example hello-world
(or maybe to have a 40 characters)?
Upvotes: 4
Views: 2685
Reputation: 514
This is an add-on to the above answers and may prove to be helpful for those who are starting with GIT.
After initializing git using 'git init'
command
each time we run a command like
echo "hello-world" | git hash-object --stdin -w
Git generates a hash for example
6b820fd9037ce516d22549dde403f3bb9a41ad8e
which is 40 characters long. This is saved in Git database; path to which is your project folder.
Go to .git folder, then go to objects folder, a folder with first two letters of generated HASH, here 6b
will be present and a file with rest 38 characters will be present inside it.
Also , if we want to unzip the HASH, run command as follows:
git cat-file 6b820fd9037ce516d22549dde403f3bb9a41ad8e -p
where -p
means print the original content, here hello-world
Upvotes: 0
Reputation: 51353
Git has a data store called object database. Commits are also objects for git and stored in this object database. Take a look at the git internals documentation for details.
Every object that is stored in the object database gets stored by it's SHA-1 hash. E.g.
echo 'test content' | git hash-object -w --stdin
d670460b4b4aece5915caf5c68d12f560a9fe3e4
Since a commit is just an object it also gets stored by it's SHA-1 hash. You can take a look at the content of a commit object using git cat-file
. Here is an example from the apache commons-io repository.
git cat-file -p 35f306967d0641e7d49cafb25938a4f69a36e77a
tree 289b9d5a11b7f52d330e86a30fd1c7d138703f4b
parent c57af0ad5604e8280884f6183e05eb30751883ef
author Sebastian Bazley <[email protected]> 1431094588 +0000
committer Sebastian Bazley <[email protected]> 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
A commit is just a plain text file as you can see. It contains
So a commit's id only changes if at least one of these attributes change.
Is it possible to force modifying a git commit id into something special, for example
hello-world
No. The commit id is a SHA-1 hash of the object's content and therefore it will only change if the object's content change. Git uses the SHA-1 to compare objects for equality. E.g. if you do a git fetch
, git can easily detect if an object is already present in your local object database.
Upvotes: 1
Reputation: 5221
Ok so let's say you are on branch develop
. Then there'll be a file called .git/refs/heads/develop
. This will store the SHA hash of the commit it points to. This, for all intents and purposes, is the entry point of all operations. Everything else done to this branch is generally relative to this SHA.
All git database is stored in .git/objects
directory. You can find the commit in there. Just split the first two letters of commit SHA and look for a directory of that name. For example, if your commit is 5f352ad1adcaf5a4bfc638d53f13db62c23d34e9
, the file might be called
.git/objects/5f/352ad1adcaf5a4bfc638d53f13db62c23d34e9
. But git does many things to save space so it might not be called that after all if your git repo had been packed. Peeking inside a file will reveal compressed unreadable data.
Obviously, you can't rename that file to something else and expect it to work. Apart from naming, the SHA hash is also used for checking the integrity of file. So a random string won't do.
Upvotes: 3