How to become a Git Dumbledore…!(The Secrets of .git Folder)
By: Ajay Negi
Tags: Git Under The Hood, Git, Internal Git, Git Commands

Dumbledore committing his magic spells on Git.

What is this blog about?

This blog is for both type of people :

Who knows how to use Git. Who doesn’t know how to use Git.

If you are first kind of the people, then you are just typing those git commands in your terminal to add,commit and alter your project data without even being sure that how this commands really work internally. I guess that is the case, right? Or else why would have you opened this blog, right ?😜.

And if you are second kind of people and just want to know how git works before learning the command, then you are at the right place, my friend 😇.

This blog will help you understand how git works under the hood

What is Git — A Brief Introduction:

Git is a version control software. “OK, But what is Version Control then?”. Well that is self explanatory, Version Control is a software that helps you keep track on the different versions of a your project/file. Quite simple, right?.

So without wasting any time lets dive into how Git works behind the scenes.

Git — Under the hood.

How does Git keep track of your project?

Probably the first question that will come in your mind is :- How does Git keep track of the files that we are saving in our local device. Well, we will get back to the answer of this question, but before that we have to make a git repository by using the command “git init”.

Initialized Git repository using “git init” command.

Notice the output in the above image when we run the command “git init”: “Initialized empty git repository in /home/ajay/Learning_Git/.git/”. It has made a empty git repository(empty because we don’t have any files in that folder yet), but the question was “how it tracks our progress in the project?”. Well, did you notice that it made a new folder named“.git” inside the “Learning_git” folder. Yes, you have guessed it right, this is the folder that connects git to your project and also notice the “.” before the folder name which means it will be a hidden folder in Mac and Linux OS, so you will not be able to see it simply unless you view hidden folders.

Three States of Git(Just a quick refresher)

Before we dive deep into how git commands work internally lets take a look towards the three states of git.Git has three main states that your files can reside in: modified, staged, and committed:

Modified means that you have changed the file but have not committed it to your database yet.

Staged means that you have marked a modified file in its current version to go into your next commit snapshot

Committed means that the data is safely stored in your local database.

This leads us to the three main sections of a Git project: the working tree(or working directory), the staging area, and the Git directory.

Three sections of git:- working directory, Staging Area and Repository.

The working tree is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.

The staging area is a file, generally contained in your Git directory, that stores information about what will go into your next commit. Its technical name in Git parlance is the “index”, but the phrase “staging area” works just as well.

The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.The basic Git workflow goes something like this:

You modify files in your working tree.

2. You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.

3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

If a particular version of a file is in the Git directory, it’s considered committed. If it has been modified and was added to the staging area, it is staged. And if it was changed since it was checked out but has not been staged, it is modified.

I found this video on udacity, which will help you understand the concept a little better and you can also join their course on git to learn about git thoroughly.

Typical workflow when working with version control.

The Mystery of the contents inside the .git folder:

When you run git init in a new or existing directory, Git creates the .git directory, which is where almost everything that Git stores and manipulates is located. If you want to back up or clone your repository, copying this single directory elsewhere gives you nearly everything you need. So, lets reveal the mystery of this .git folder by using the tree .git command.

The output of "tree .git" showing the contents of .git folder.

Lets have a brief overview about what are these folders are for: the description file is used only by the GitWeb program, so don’t worry about it. The config file contains your project-specific configuration options, and the info directory keeps a global exclude file for ignored patterns that you don’t want to track in a .gitignore file. The hooks directory contains your client- or server-side hook scripts.

This leaves four important entries: the HEAD and (yet to be created) index files, and the objects and refs directories. These are the core parts of Git. The objects directory stores all the content for your database, the refs directory stores pointers into commit objects in that data (branches, tags, remotes and more), the HEAD file points to the branch you currently have checked out, and the index file is where Git stores your staging area information. Now we will only talk about some of its contents which are really important to understand.

Now, lets make a file to add and commit for our investigation to go forward by using the command touch .

Created an empty Readme file using touch command.

Remember this that we have only created this file and it is untracked by the git because we have not added and committed it yet. You don’t believe me?. OK, then lets confirm it with using the command git status.

Git status showing that the Readme file is untracked by it.