Inside Git: How It Works and the Role of the .git Folder
Source: Dev.to

Most developers use Git every day with commands like git init, git add, and git commit, but only a few know how Git actually handles these commits internally.
In this blog we’ll look inside Git, see how it works internally, and explore the important role of the .git folder.
What is Git? (Quick Recap)
Git is a version‑control system that helps developers track changes, collaborate, and manage project history efficiently.
If you want to learn more about Git, you can read this blog too. 👇
How Git Works (Internal View)
Git works in three main areas:
- Working Directory – Your actual project files.
- Staging Area – Where changes are prepared.
- Repository – Where Git permanently stores snapshots.
Every time you commit, Git:
- Takes a snapshot of all tracked files.
- Stores it safely inside the
.gitdirectory. - Links commits together.

The Heart of Git: The .git Folder
When you run:
git init
Git creates a hidden folder inside your project root. It’s there—you just can’t see it in the file explorer.
👉 This folder is the entire brain of Git.
If it’s deleted, your project is no longer a Git repository.
What’s Inside the .git Folder?
You can also view it from the command line:
cd .git
ls -la
objects/ – Where Everything Is Stored
This directory holds all Git data: commits, files, and directory structures.

- Commit objects – snapshots of the repository at a point in time.
- Tree objects – represent directory hierarchies.
- Blob objects – store file contents.
Note:
git logshows only commit objects, while.git/objectsalso contains trees and blobs, which is why you’ll see more directories than commits.
refs/ – Branch & Tag Pointers
This folder keeps references to branches and tags.

- Branches (
refs/heads) – point to the latest commit of each branch. - Tags (
refs/tags) – mark specific commits.
Each branch is simply a pointer to a commit hash.

HEAD – Where You Are Right Now
The HEAD file tells Git:
- Which branch you are on.
- Which commit is currently checked out.

index – The Staging Area
The index (or staging area) stores:
- Files you added with
git add. - The exact snapshot that will become the next commit.
It acts as a bridge between the working directory and the repository.
config – Repository Settings
The config file contains:
- Remote URLs.
- User configurations (name, email).
- Repository‑specific settings.

TL;DR
- Git stores everything in the hidden
.gitdirectory. objects/holds the raw data (commits, trees, blobs).refs/tracks branches and tags.HEADpoints to the current branch/commit.indexis the staging area.configholds repository configuration.
Understanding these components demystifies how Git tracks changes and why operations like git commit, git checkout, and git push work the way they do. Happy committing! 🚀
The Basic Workflow
# Initialize a git repository
git init
# Stage changes
git add .
# Commit the staged changes
git commit -m "message"
What Git Does
- Initializing the repository creates the
.gitdirectory. git addstores the staged files as blob objects in.git/objects/.git commit -m "message"builds a tree object that represents the directory structure.- Git then creates a commit object containing:
- A pointer to the root tree.
- A pointer to the parent commit (if any).
- Author and committer information.
- A timestamp.
- The commit message.

Image credit: Photo by freeCodeCamp
Final Thoughts
Git may look simple on the surface, but internally it’s a powerful database.
The .git folder is not just a config folder; it contains the entire history and structure of your project.
Thanks for reading! If you enjoyed this post, you can read more on the topics below:
