Skip to main content

Command Palette

Search for a command to run...

Understanding Git: A Deep Dive into the .git Folder and Its Functionality

Published
•2 min read•View as Markdown
Understanding Git: A Deep Dive into the .git Folder and Its Functionality
  1. How Git Works Internally

  • first, when we git init then a .git/ named folder automatic created, which contains history, commits, and branches

  • Git has 3 area

1st- Working directory — The area where the work place, where we write the code like git init, git add, git commit -m “msg” is called the working directory

2nd- Staging area — After adding the git it appears here, and the code is ready to be committed

3rd- Repository(Repo) — Everything is here like history, commit, branches

git commit -m “msg”

  • git add, What does it do internally ?

-git init adds the file to the staging area , It has not been committed yet

  • git commit, What does it do internally ?

-Tree is formed ,

  • Creates commits ob

  • jects,

-Links to the parent commit,

-Updates the head,

  • What is head ?

  • This tells us which branch/commit we are currently on

short flow

git init ==> git add ==> staging (Index) ==> git commit ==> head change

2. Understanding the .git Folder

The .git folder is a hidden directory that contains all the necessary information for a git repository to function . It is essentially the database and control for your entire project, tracking change, commit, and branch

Why the .git folder is exist

The .git folder is exists to enable git’s core functionalities:

(1). Version control

(2). Offline works

(3). Collaboration

(4). Data integrity

3. Git Objects: Blob, Tree, Commit

https://shafiul.github.io/gitbook/assets/images/figure/objects-example.png

Blob :-

  • blobs stores only the content of the file

  • Data inside the file = Blob

  • File name not stored in git blob

Tree:-

  • Tree object is like a folder

  • Which file is it

  • Which folder is it

  • File name

  • Blob hash of file : index.js, app.js==>blob hash

Commit:-

  • Reference to a tree

  • Parent commit (Previous version)

  • Author

  • Date & Time

  • Commit message

commit==>tree==>blob

https://thoughtbot-images.s3.amazonaws.com/upcase/git-course/git-base-object-model.png

4. How Git Tracks Changes:-

Git snapshot leta hai

  • Geet takes a snapshot of the entire project on every commit.

  • Agar koi file change nahi hui, to Git uska reference reuse karta hai (duplicate copy nahi banata).

Content-based tracking

  • Git file ke content ka hash banata hai like (SHA-1 / SHA-256)

  • If even one character changes in the content, the hash will change

How .git directory structure works

Relationship between commits, trees, and blobs

Internal flow of git add and git commit