DEV Community

Cover image for Insights of git (part :2)
bhushan mhaske
bhushan mhaske

Posted on

Insights of git (part :2)

introduction

We all use 'git' and it's features in daily basis while doing code or working on projects , in-fact it is essential. But , many of us just use git commands they don't understands what actually happens inside the .git repository when they use commands like git init ,git add ,git commit ,git push etc..
Let's discuss the behind the scene of git .

The .git Folder
when we use command git init the .git folder creates in your project .
your project folder is not a repository it's just a working directory ,
.git folder is actual respository.

see below the structure of .git folder (repository).

let's discuss one-by-one -

1.HEAD - head is just pointer which points to the current checked-out branch (or) latest commit

2.config - config file contains repository specific settings

3.index - index file is the staging area , basically stores the stage files.

4.object - objects stores the actual data , there are 3 types of objects - blob , tree , commit.

5.refs - gives the references about the tags and branch.

6.log - log file stores the all history of commits.
this is the basic git folder structure.

git objects - Blob , Tree , Commit.

  • whatever we stores in git using git commit -m "message" command the git stores it as a object .

1] Blob - it stores the content of a file . and doesn't care about file name and all , only stores the content.

2] Tree - represents the structure of a directory .

3]Commit - snapshot of a project at a point of time
- includes Author,Date & Time,hash,parent hash,Commit message

What happens internally during git add and git commit


What happens during git add
when u run git add file.txt

-Git takes the content of the file and creates blob objects for the current file content.

-Then Git generates a unique SHA-1 hash (a 40-character string) based on the file content. The first two characters of the hash become the folder name, and the remaining 38 become the filename.

-Finally, Git updates the staging area (index) to track the snapshot that will be committed.

What happens during git commit
when u run git commit -m "message"

-It transforms what’s in the staging area into a snapshot, which is stored in the Git repository history.

-Each commit is given a unique hash to identify it.

-The -m flag allows you to include a message directly in the command.

-Commits should always have a descriptive message.

Conclusion

Gitis a powerful version control system that operates as a key-value database, ensuring the reliability and integrity of your project versions.

The .git folder is the core of your Git repository, storing the entire history, branches, and snapshots of your project. Understanding the internal workings of Git, including its objects like blobs, trees, and commits, as well as the processes of git add and git commit

Top comments (0)