[!NOTE] “If you know the enemy and know yourself, you need not fear the result of a hundred battles.” — Sun Tzu
To master Git, you must know what lies inside the.gitfolder.
Every time you run git init, Git creates a hidden directory called .git. This is your repository. Your working directory is just a temporary checkout of the data stored in here.
If you delete this folder, you lose your project’s history.
The Anatomy of .git
Let’s dissect a typical .git folder.
Key Components
1. HEAD
This is a simple text file that tells Git where you are.
Usually: ref: refs/heads/main
Detached: a1b2c3d... (direct SHA-1)
2. objects/
This is the database. Git is a key-value store.
- Key: SHA-1 Hash
- Value: Compressed content
Git doesn’t store “files” and “folders” like your OS. It stores Objects.
3. refs/
Short for “references”.
refs/heads/: Local branches.refs/heads/mainis just a file containing a commit hash.refs/tags/: Tags (release points).refs/remotes/: Read-only pointers to remote branches (e.g.,origin/main).
4. index
Also known as the Stage. When you run git add, Git:
- Hashes the file content.
- Stores the object in
objects/. - Updates the
indexentry to point to that new object.
This is why git status is so fast—it compares the index (stat cache) against your working directory.
Summary
The .git directory is not black magic. It is a simple structure of:
- Text files (refs, config, HEAD)
- Binary files (index)
- Compressed objects (objects/)
Understanding this folder removes the fear of “breaking” Git. Everything is recoverable if you know where to look.