{{announcement.body}}
{{announcement.title}}

A Bit About Git

DZone 's Guide to

A Bit About Git

This article tries to explain a little bit about the concepts on which Git works.

· Java Zone ·
Free Resource

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.

Shell
 




xxxxxxxxxx
1


1
$ mkdir hellogit
2
$ cd hellogit
3
$ git init
4
Initialized empty Git repository in hellogit/.git/


Now let's create a file called helloworld.txt using echo command.

Shell
 




xxxxxxxxxx
1


1
$ echo 'Hello, git!' > helloworld.txt
2
$ ls
3
helloworld.txt


Git holds all the objects (commits,trees,blobs) under  .git/objects  folder. Let's see what's in there before we commit the  helloworld.txt  file. 

Shell
 




xxxxxxxxxx
1


1
$ ls .git/objects/
2
info/  pack/


As can be seen, there are no objects created yet.

Let's commit the changes to Git.

Shell
 




xxxxxxxxxx
1


 
1
$ git add helloworld.txt
2
warning: LF will be replaced by CRLF in helloworld.txt.
3
The file will have its original line endings in your working directory
4
 
          
5
$ git commit -m 'initial commit'
6
[master (root-commit) bd3ed27] initial commit
7
 1 file changed, 1 insertion(+)
8
 create mode 100644 helloworld.txt


Let's see what is created in the .git/objects folder now.

Shell
 




xxxxxxxxxx
1


1
$ ls .git/objects/
2
65/  bd/  ee/  info/  pack/


Now, there are three objects created (apart from info,pack).

Let's check the git log to see the commit id.

Shell
 




xxxxxxxxxx
1


1
$ git log --oneline
2
bd3ed27 (HEAD -> master) initial commit


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.

Shell
 




xxxxxxxxxx
1


1
$ git cat-file -p bd3ed27
2
tree ee36d9e1cc71be95844033e37a8ebb966f090383
3
author Abc Xyz 1590934838 +0530
4
committer Abc Xyz 1590934838 +0530
5
 
          
6
initial commit


Now this printed the SHA-1 ID of tree it is referencing. 

Shell
 




xxxxxxxxxx
1


1
$ git cat-file -p ee36d9e1cc71be95844033e37a8ebb966f090383
2
100644 blob 6566899ce31c049799f8f79c2ec72092ddc31fc8    helloworld.txt


 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

Shell
 




xxxxxxxxxx
1


1
$ git cat-file -p 6566899ce31c049799f8f79c2ec72092ddc31fc8
2
Hello, git!
3
  
4
$ cat helloworld.txt
5
Hello, git!


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.

Topics:
blob, commit, git, java, open source, tree, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}