
Module 2 Lesson 3: Repository structure (.git folder)
Demystify the 'Brain' of Git. Explore the contents of the hidden .git directory—from the object database to the config file—and learn how Git stores your history.
Module 2 Lesson 3: Repository structure (.git folder)
Every Git repository has a secret. This secret is a hidden folder called .git.
For most users, this folder is a "black box" that they should never touch. But for a Git Master, understanding this folder is the difference between "using Git" and "understanding Git." In this lesson, we peek under the hood.
1. Where is it?
When you run git init or git clone, Git creates a .git directory at the root of your project.
- Working Directory: The files you see and edit.
- Git Directory: The hidden
.gitfolder that tracks everything.
Crucial Rule: Never delete the .git folder unless you want to permanently erase the history of that repository and turn it back into a regular folder.
2. Key Components of the .git Folder
If you open the .git folder, you will see several files and subdirectories. Here are the most important ones:
The config file
This is the repository-specific configuration file. It stores things like the URL of your remote repositories (GitHub/GitLab) and any special settings for this project.
The objects/ directory
This is the heart of Git. It is the database that stores the compressed content of every file, every commit, and every tree in your history. Every "object" is named with a unique SHA-1 hash (a 40-character string).
The refs/ directory
This stands for "references." It stores pointers to your commits.
refs/heads/: Contains files named after your branches (likemain). Inside each file is the SHA-1 hash of the most recent commit on that branch.
The HEAD file
The HEAD file is a special pointer that tells Git what you are currently working on. Usually, it points to a branch (e.g., ref: refs/heads/main). When you switch branches, Git simply updates this file.
The index file
As we learned in Module 1, this is your Staging Area. It’s a binary file that tracks what you have staged for your next commit.
graph TD
Root[".git Folder"]
Root --> Config["config (Settings)"]
Root --> Objects["objects/ (Database)"]
Root --> Refs["refs/ (Pointers)"]
Root --> HEAD["HEAD (Current Branch)"]
Root --> Index["index (Staging Area)"]
Refs --> Heads["heads/ (Local Branches)"]
Heads --> Main["main"]
Heads --> Dev["feature-x"]
3. How Git Stores Data (Snapshots, not Diffs)
Unlike older systems that only store the "changes" (diffs) between files, Git stores snapshots.
- If a file hasn't changed between versions, Git doesn't save a new copy; it simply creates a link to the previous version of the file that is already in its database. This makes operations like switching branches incredibly fast.
Lesson Exercise
Goal: Inspect your local repository's "Brain."
- Go to the
git-practicefolder you created in Lesson 1. - Open the
.gitdirectory. - Open the
configfile in a text editor. - Now, open the
HEADfile. What does it say? (It likely saysref: refs/heads/mainormaster). - Open the
descriptionfile. Does it contain anything useful? (Standard Git repos usually just have a placeholder here).
Observation: You'll see that Git is remarkably simple—it’s just a collection of text files and a compressed database that keeps track of your pointers.
Summary
In this lesson, we established:
- The
.gitfolder is the "Brain" and database of your project. objects/is where the actual compressed content lives.refs/andHEADmanage the "navigation" of your history and branches.configstores project-specific settings.
Next Lesson: We’ll learn how to keep the "Brain" clean by teaching Git which files it should ignore using .gitignore.