Understanding .git Folder
4.1 Overview
Every Git repository has a hidden .git folder at its root.
This folder is the heart of Git — it stores all metadata, commit history, branches, and configuration details. Deleting it will erase the entire version history, effectively turning your project into a regular, non-Git folder.
This folder is the heart of Git — it stores all metadata, commit history, branches, and configuration details. Deleting it will erase the entire version history, effectively turning your project into a regular, non-Git folder.
4.2 Where It Appears
When you initialize a repository using:
Git creates a .git folder automatically:
You can reveal it with:
1git initGit creates a .git folder automatically:
1my-project/
2│
3├── .git/
4├── index.html
5├── style.css
6└── script.jsYou can reveal it with:
1ls -a4.3 Internal Structure
Let's explore what's inside .git/ and what each component does:
| Folder/File | Purpose |
|---|---|
| HEAD | Points to the current branch you’re on. (e.g., ref: refs/heads/main) |
| config | Stores repository-specific configuration (user name, email, remotes, etc.) |
| description | Used by Git web interfaces; mostly unused in local repos. |
| hooks/ | Contains scripts that run on specific Git events (e.g., before commit). |
| info/ | Includes additional info like the global exclude file (untracked ignores). |
| objects/ | Stores all commits, trees, and blobs — the actual data of your project. |
| refs/ | Contains references (pointers) to commits: branches, tags, and remotes. |
| logs/ | Records every update made to refs (useful for recovering lost commits). |
| index | Tracks what’s currently in the staging area. |
4.4 Example: The HEAD File
The HEAD file tells Git where you are in the project's history.
Example content:
This means you're currently working on the branch named main.
When you check out another branch, Git updates the HEAD file accordingly.
Example content:
1ref: refs/heads/mainThis means you're currently working on the branch named main.
When you check out another branch, Git updates the HEAD file accordingly.
4.5 The "objects" Folder - Git's Database
Inside objects/, Git stores everything as compressed snapshots:
• Blobs → file content
• Trees → directory structure
• Commits → references to trees and parent commits
Each object is identified by a SHA-1 hash (a unique ID).
Example object name: b1/3f9c4d1e2a8d...
This structure allows Git to store massive histories efficiently without duplicating data.
• Blobs → file content
• Trees → directory structure
• Commits → references to trees and parent commits
Each object is identified by a SHA-1 hash (a unique ID).
Example object name: b1/3f9c4d1e2a8d...
This structure allows Git to store massive histories efficiently without duplicating data.
4.6 The "refs" Folder - Pointers to Commits
References (or refs) act like bookmarks to specific commits:
• refs/heads/main → current branch's latest commit
• refs/tags/v1.0 → commit tagged as version 1.0
• refs/remotes/origin/main → last fetched commit from the remote repo
You can inspect them manually:
This outputs a commit hash.
• refs/heads/main → current branch's latest commit
• refs/tags/v1.0 → commit tagged as version 1.0
• refs/remotes/origin/main → last fetched commit from the remote repo
You can inspect them manually:
1cat .git/refs/heads/mainThis outputs a commit hash.
4.7 Hooks Folder (Automation Scripts)
hooks/ contains example scripts for automation.
For instance:
• pre-commit → Runs before a commit
• post-merge → Runs after merging branches
To activate one, remove the .sample extension and make it executable:
For instance:
• pre-commit → Runs before a commit
• post-merge → Runs after merging branches
To activate one, remove the .sample extension and make it executable:
1chmod +x .git/hooks/pre-commit4.8 Common Troubleshooting Tip
If your repository behaves oddly or shows missing history, never delete .git/.
Instead, inspect it safely:
This command checks for any integrity issues in Git's internal data.
Instead, inspect it safely:
1git fsckThis command checks for any integrity issues in Git's internal data.
4.9 Key Takeaways
• .git/ is the repository's brain — it holds all history, metadata, and configuration.
• Deleting or moving it disconnects your project from version control.
• The objects/ and refs/ directories power Git's entire commit and branch system.
• HEAD shows which branch you're on; index tracks what's staged.
• Understanding .git helps you debug, recover commits, and use advanced Git commands confidently.
• Deleting or moving it disconnects your project from version control.
• The objects/ and refs/ directories power Git's entire commit and branch system.
• HEAD shows which branch you're on; index tracks what's staged.
• Understanding .git helps you debug, recover commits, and use advanced Git commands confidently.