A Bit About Git
A Bit About Git
This article tries to explain a little bit about the concepts on which Git works.
Join the DZone community and get the full member experience.Join For Free
This article assumes that you have working experience with Git.
Git is one of the most used versioning systems. Git works on three main concepts: Commit, Tree, Blob.
Commit: When a commit is done on a Git repo, a snapshot of the files is taken at that point in time.
A commit holds reference to a tree, commit message and reference to the parent commit. This creates the revision history i.e. one commit pointing to the parent/previous commit.
Tree: Tree is nothing but a folder. A Tree can hold a reference to a list of Blobs or other Trees.
Blob: Blob is nothing but the file.
When a commit is made to a Git repo, all the 3 are created: a Blob, a Tree pointing to the Blob, and a Commit pointing to the Tree. And each of these are given SHA-1 ID.
Let's explore this with an example.
Let's create a folder named "hellogit" and initialize git with the
git init command.
Now let's create a file called
helloworld.txt using echo command.
Git holds all the objects (commits,trees,blobs) under
.git/objects folder. Let's see what's in there before we commit the
As can be seen, there are no objects created yet.
Let's commit the changes to Git.
Let's see what is created in the .git/objects folder now.
Now, there are three objects created (apart from info,pack).
Let's check the git log to see the commit id.
With the commit id bd3ed27 , let's try to explore the Commit->Tree->Blob connection.
There is a git command called 'cat-file' that helps us identify this connection.
Now this printed the SHA-1 ID of tree it is referencing.
As you can see, the cat-file command on the Tree printed its details which includes the files it is referencing and the SHA-1 ID of that file(blob).
Let's do cat-file on that blob
As you can see, the content in the blob is same as the content that is held by the file helloworld.txt.
This is how Git manages the contents in the repository internally, through Commit, Tree, Blob.
Opinions expressed by DZone contributors are their own.