Keys and Values
Keys in SHA1
As I mentioned that Git basic concept is table where it maps keys with values. Values are just sequence of bytes.
here keys are SHA1 and values are piece of content
Enter below command in terminal for hash of word or say object "Mutiny"
echo "mutiny" | git hash-object --stdin
- --stdin : standard input or getting direct input instead from a file
- hash-object : calculates object ID
Now lets save this hash value and see where git stores and how it stores
echo "mutiny" | git hash-object --stdin -w
Now, lets see where this hash object is being stored.
Before learning about what commit is, I would like to tell you about Git objects which are present in .git/objects. Git hash four types of objects: blobs, trees, commits and tags.
File which contain any data is called blob.
Example: addr.txt in our case
Tree is basically a directory which contains blob or directory(tree).
Example: Detail is a tree in our case which further contains two blobs(hobby.txt,intro.txt)
A commit holds metadata for each into the repository including the author, committer, commit date and log message. Commits also have parents(later explained in Keys and Values).
A name which is assigned to a commit. Its difficult to remember a commit hash, so we provide a name to hash just like a Domain name to IP.
When you first initialize git for new repository, its objects folder does not store any hash key, even if you have any file or folder already present in your initialized directory. For example :
As in above image, we can see there is one file and folder already present. And when we check our object directory under .Git, we find nothing other then info and pack.
After we commit it, it generates hashes and stores them in objects folder. But,
what commit exactly is ? It is used to records changes in our repository with an optional message.
To commit, we first need to add all changed files to staging area where they are saved using commit.
git add .
git add addr.txt detail
To check whether they are staged or not use command.
If it shows files and folder under "Changes to be commited" in Green means they are properly staged.
Now inorder to commit :
$ git commit -m "first commit
Now go to object folder and you will find the hashes of files being committed. See logs inorder to see, which hash is of the commit we did. From there you can find hash of all the files and folder which you committed.
In order to see logs, we use command
$ git log # or $ git log --oneline
Navigate to .git/objects You will find following directories
If you carefully notice the starting 2 letters of hash in the commit message which is 9fab911.....f08 i.e, 9f. There is a directory present in .git/objects with the same 2 letters.
When you open this directory, you will find a hash. Lets see what is inside this hash.
$ git cat-file -p 9fab911ea
We see that this hash contains metadeta about the commit.
- There is a tree with some hash value
- Author and committer details
- Message of commit
Now lets further explore the Tree hash and see what it contains.
$ git cat-file -p 0b5ae6a8a
We see another Tree hash and blob hash.
This is how we can track the data which is stored inside git repository. And this is the way using which git does tracking which we call Control System
At end this is how git maps commits, blob,trees and tags
To see the same in slides, click here
Note: If you are replicating the same scenario. Hash of your tree, commit and blob will have different hashes as it depends on the value inside them. In your case commit value will change due to different author, committer. Blob hash will also vary due to different content inside the txt files.