How Git works under the hood (and why that saves you scares)
Published on 2026-09-12 by frfrand
Git does not store differences, it stores full snapshots. That explains why branches are cheap, why almost nothing is lost, and what each command does.

Short answer: Git stores each commit as a complete snapshot of the file tree, plus a pointer to the previous commit; differences are calculated on the fly. That's why a branch is just a pointer, creating one is instantaneous and almost nothing is lost: git reflog remembers where HEAD has been and allows you to go back.
You can use Git for years with five memorized commands and without understanding what it does. It works, until the day something goes wrong and you don't know why.
Understanding the model takes ten minutes and changes your relationship with the tool.
The three places
Every Git command moves something between three spaces:
The working directory. The files as they are now on your disk.
The staging area. An intermediate zone where you prepare the next commit. When you run git add, you move changes here.
The repository. The confirmed history. git commit takes what is in staging and saves it.
That intermediate zone is confusing at first and is exactly what makes Git powerful: it allows you to choose which part of your work goes into each commit. You can have five modified files and confirm only two, or even just some lines of one with git add -p.
A commit is not a difference
Here is the most common misunderstanding. Most people think a commit stores "what changed". It does not.
A commit stores a complete snapshot of the file tree at that moment, plus a pointer to the previous commit and some metadata.
The differences you see in git diff or on GitHub are calculated on the fly, comparing two snapshots.
Doesn't that take up a lot of space? No, for two reasons: files that haven't changed are reused (Git stores contents by their hash, so the same content is stored only once), and then everything is compressed.
From that design come two important consequences:
- Branches are dirt cheap. A branch is a 41-byte file (40 SHA-1 hash characters and a newline) that points to a commit. Creating one is instantaneous. That's why Git's workflow encourages creating branches for everything, something unthinkable in previous systems.
- History is verifiable. A commit's hash is calculated from its content, which includes the parent's hash. Changing something old changes all subsequent hashes. That's why rewriting shared history causes so many problems.
HEAD, branches, and "detached HEAD"
HEAD is a pointer that says where you are.
Normally HEAD points to a branch, and the branch points to a commit. When you make a new commit, the branch advances and HEAD goes with it.
If you checkout a commit directly, HEAD points to the commit without going through a branch. That's the famous "detached HEAD". It's not broken: simply, if you make commits there and leave, those commits don't have any branch holding them. That's why the warning.
The command that saves the day
This single fact justifies the whole article.
Git keeps a record of all the positions HEAD has been in: the reflog.
git reflog
If you did a reset you shouldn't have, if you deleted a branch by mistake, if you got lost in the middle of a rebase, there is the list of where you were, with its hash. Going back is:
git reset --hard <hash>
Orphaned commits remain in the repository for weeks before the garbage collector cleans them up. Almost nothing is truly lost in Git. What is lost are changes that were never committed, and for that there is the habit of making small and frequent commits.
What is good to keep in mind
- A commit is a snapshot with a pointer to the parent.
- A branch is a pointer to a commit, and it is free.
- HEAD says where you are.
- The reflog remembers where you were.
With that, commands stop being magic formulas and become operations you can reason about.


From Git history to the production server
Git stores objects and references to identify versions. This feature is especially useful when deploying an application: by noting the commit hash you can know exactly which version of the code is running.
A reasonable flow is to test the change on a branch, review it via pull request, merge it, and deploy from a Git repository. In ArduMaker you can configure repository deployment on your VPS: each deploy notes the commit that ended up in the folder and keeps its history by stages, so you can know which version was running when an error appeared. The repository does not replace database backups.
Related: What a commit stores · Pull requests.
Frequently asked questions
Does Git store differences or full files? It stores full snapshots of the tree, but reuses unchanged content (identified by its hash) and compresses everything, so it takes up little space.
What is a detached HEAD? Being positioned on a commit without a branch holding it. It's not broken, but if you make commits there and leave, they end up without a branch; create one with git switch -c before leaving.
How do I recover a deleted branch or an incorrect reset? With git reflog you find the hash of where you were and return with git reset --hard <hash> or create a branch at that point. Orphaned commits are kept for a while before the garbage collector cleans them up.