Skip to main content

Command Palette

Search for a command to run...

Inside Git - How It Works and the Role of the .git Folder | Git & GitHub

Updated
6 min readView as Markdown
Inside Git - How It Works and the Role of the .git Folder  |  Git & GitHub
A
CS student and full-stack developer from Pakistan. Building with JavaScript, TypeScript, React, Next.js and Node.js. Currently in ChaiCode Web Dev Cohort 2026.

We use git init, git add, git commit almost every day. But if someone stopped you right now and asked "where does Git actually keep all that history?" — could you answer?

Most people can't. And that's okay, because Git hides everything inside one small folder that almost nobody ever opens: .git.

Once you see what's inside it, Git stops feeling like magic. It just becomes a folder with a few simple jobs.


Where everything actually lives

The moment you run git init, Git quietly creates a hidden folder called .git. This folder is not just some settings file — it's basically the whole brain of Git. Every commit, every branch, every bit of history you have ever made lives inside it.

Try this in your terminal:

ls -a

You'll see .git sitting there with the other hidden files. Here's the part that shocks people the first time: if you delete this folder, Git is gone. Your actual code files stay, but every commit and every bit of history disappears right away. There's no trash bin for it. It's just gone.

So feel free to look around inside it (that's actually a great way to learn), just don't delete it.

Three things inside .git that really matter

There's a lot of stuff inside .git, but only three things matter if you want to understand how Git remembers everything: HEAD, refs/heads/main, and objects/.


HEAD — tells you where you are

Open it:

cat .git/HEAD

You'll see something like:

ref: refs/heads/main

That's all HEAD does. It just says "you're standing on the main branch right now." Nothing about history, nothing fancy. Just your current location.


refs/heads/main — points to your latest commit

Now check the file HEAD was pointing at:

cat .git/refs/heads/main

You'll see a long string of letters and numbers — a commit hash, something like a1b2c3d4.... This is your latest commit. Just the latest one, not the whole history.

You can prove this yourself. Run:

git log --oneline

Look at the top line. That hash is the exact same one sitting inside refs/heads/main. Git isn't searching around to find your latest commit every time you run a command — it just reads it straight from this one small file.


objects/ — where the real data is stored

The .git/objects/ folder holds every commit and every file snapshot you've ever made — but not as readable files you can just open. Everything here is compressed and saved under its own hash name.

There are three kinds of things stored inside .git/objects/ folder :

  1. Commit objects (they hold the author, message, date, and a pointer to the commit before it)

  2. Tree objects (a snapshot of what your folders and files looked like at that moment)

  3. Blob objects (the actual content of your files).

The three types of objects Git stores inside .git/objects/.


So how does git log build your history?

Git doesn't have some master file listing all your commits in order. There's no such thing anywhere in .git folder. Instead, Git rebuilds your history every single time by walking backward, one commit at a time, like following footsteps.

Here's what actually happens:

  • Git checks HEAD first to see which branch you're on.

  • Then it opens refs/heads/main to get the hash of your latest commit.

  • Then it opens that commit inside .git/objects/. And every commit stores one more thing — the hash of the commit right before it, called its parent.

So Git opens your latest commit, reads its parent hash, jumps to that one, reads its parent, jumps again — and keeps doing this over and over until it hits the very first commit ever made, the one with no parent.

That's your whole history. It's not stored as a list anywhere. It's just commits pointing back to the one before them, and Git walking that chain backward, one step at a time.

You can even watch this happen yourself:

git cat-file -p a1b2c3

You'll see the message, the author, and a parent line pointing to the commit before it. Take that hash, run the same command on it, and you're literally walking the same path Git walks internally.


What if the ref disappears?

Here's a question worth thinking about. If refs/heads/main gets deleted, does your history disappear too?

No. All your commits are still sitting safely inside .git/objects/, untouched. What's actually missing is the starting point. Git no longer knows which commit was the "latest" one, because that was only ever stored in this one small pointer file. Without it, git log has nowhere to begin, even though the full chain is still sitting right there on your disk.

Good news — this is usually fixable. Git keeps a backup record called the reflog:

git reflog

This shows you where HEAD has recently pointed, so you can find your last known commit hash. Then you can rebuild the branch by hand:

git branch main <recovered_hash>

Once that ref exists again, git log can start walking the chain just like before.


Git vs GitHub

While we're here — a lot of people put "Learned GitHub" on their resume, which is actually a bit off. GitHub is just where you host your project online. Using it well is mostly clicking buttons and following a workflow. Git is the real tool underneath — the commands, the object storage, the whole chain we just walked through. If you're learning about commits and branches and history, you're learning Git. GitHub is just where you send that history once you're ready to share it.


Why this matters

Once you understand .git this way, a few things stop being confusing. Your history is safe as long as this folder exists — nothing else needs to be running. The folder is completely self-contained, so you can zip your project, move it anywhere, and it still works exactly the same. And Git needs no internet connection to do any of this. GitHub only comes into the picture when you want other people to see your work too.

Every command you run — git log, git diff, git checkout — is really just Git reading these same three pieces. HEAD tells it where you are, the ref tells it where your latest commit is, and the objects folder holds the chain it walks backward from there.


You can find more of my work at abdulrdeveloper.me

Read more posts at blog.abdulrdeveloper.me

More from this blog

A

Abdul Rahman

16 posts

Writing about web development as I learn — projects, mistakes, and things worth sharing. CS student. ChaiCode Web Dev Cohort 2026.