What the Hell is Git….!

If you already used popular git commands in your projects and you are curious to know how actually git works internally….Welcome to the 10% club .I hope you have somewhere heard “Git is nothing But As Distributed Version control System” have you wonder how actually versioning happens internally. If not you are in the right place. Today i am going to explain what git actually is lets begin..

At the low level git is nothing but as key value(map) data structure now lets create one empty folder initialize the folder with git.

$ mkdir Demo
$ cd Demo
$ git init

Above Command first creates folder named “Demo” and initialize that empty folder with git. lets discuss what happened after initialization. Above command will create folder named “.git” if you are not seeing that folder don’t get surprised by default folder was hidden and you should enable hidden files visible in your file explorer .let look inside “.git” folder and understand what actually is.

Understanding “.git” Folder Structure

config →It contains Repository Information. this configuration file is created for each individual project. entries in this file will overwrite the entries in the .gitconfig file of your main directory. it’s look like below.

HEAD →It behaves like pointer. It points to the current working branch by default it will point master branch. if we read the file it’s look like as below.

hooks →Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. You can use these hooks for all sorts of reasons. It’s looks as below. these are the predefined hooks added by git they are coming from “C:\Users\{username}\.git\hooks” these are added while you installed git software .By default all hooks are disabled we should remove” .sample ” extension to make it run.as you carefully observe some of the files prefixed with “pre” some of the file prefixed with “post” it means it instructing git execute those scripts before or after any event occurred (Ex:- git commit, git push).soon I will make another story on how to write custom hooks.

objects →It contains Files with content as key value pair(key=”filename” ,value=”file content”) stored in the form of blob object. inside objects folder it contains “info” and “packs” subdirectory which we can discuss later.

refs → It contains “head” and “tags” as subdirectory head subdirectory contains list of branches in project. In our case it shows as below.

by default git creates master branch(if you are not seeing in your repo just add first commit after git will create the branch)for more clarity just create one extra branch.

Now we are created one branch named “Development” lets check in the heads folder.

As you see above newly added “Development” branch appearing inside heads folder. inside files there is no fancy stuff straight forward “SHA-1” key will be present we will discuss in deep in next discussion about “SHA-1” key what it is ,why it is generated and where it was used. If we look inside “Development” it shows like this.

As I said it just a “SHA-1” key generated by git we will discuss in deep in next section. we left “tags” subdirectory lets discuss about that directory. The tag object is very much like a commit object — it contains a tagger, a date, a message, and a pointer. The main difference is that a tag object generally points to a commit rather than a tree. It’s like a branch reference, but it never moves — it always points to the same commit but gives it a friendlier name. git has two types of tags .

Lightweight tag

These are the simplest way to add a tag to your git repository because they store only the hash of the commit they refer to. They are created with the absence of the -a, -s, or -m options and do not contain any extra information. lets start to create one lightweight tag. For creating tag just run below command.

$ git tag Dev

Above Command will create lightweight tag named “Dev” inside tags folder . now tags folder will contain object named “Dev” as you seen below.

Inside Dev object it contains it contains “SHA-1” key which points to the current latest commit in the project. it look’s like below.

If we now try to log using “git log” it contains our tag name and which branch Head currently pointing and information about the commit.it looks like as shown below. this is all about lightweight tags lets jump to annotated tag.

Annotated tag

Annotated tags store extra metadata such as author name, release notes, tag-message, and date as full objects in the Git database. All this data is important for a public release of your project. we can create annotated tag by running below command “-a” specifies annotated and “-m” specifies message.

$ git tag -a Prod -m "Iam Annotated tag"

Above creates one annotated tag with tag name “Prod” and with some metadata “I am Annotated tag” the main difference between lightweight and annotated tag lightweight tag directly refers to the commit but annotated tag create reference to the commit ( bridge between them) we will discuss in deep coming stories. if we try to open “prod” tag object it looks like as below.

unlike lightweight tag it will not directly point to commit “SHA-1” key instead it will create reference these references are created inside objects folder.

As you see starting two digit of “SHA-1” key appeared as directory name remaining as file inside file it contains “SHA-1” key of commit now this file is bridge between commit and tag but in case of lightweight tag there is no bridge tag itself directly points to the commit.

I Hope You understood the complete folder structure of git soon I will be making story internal working of git .Follow for more technology related stuff

Bye Bye…..!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek N

Abhishek N

Abhishek is a Software developer who is passionate about learning technology related stuff and implementing it in real world.