0
0
Gitdevops~5 mins

The .git directory structure - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a Git repository, it stores all its data and history inside a hidden folder called .git. This folder keeps track of your project changes and helps Git manage versions safely.
When you want to understand how Git saves your project history and files internally
When you need to troubleshoot Git issues related to repository data
When you want to manually inspect or backup your Git repository data
When you want to learn how Git organizes commits, branches, and configurations
When you want to clean or reset parts of your Git repository safely
Commands
This command creates a new Git repository in the folder 'my-project' and sets up the .git directory inside it to store all Git data.
Terminal
git init my-project
Expected OutputExpected
Initialized empty Git repository in /home/user/my-project/.git/
This lists all files and folders inside the .git directory so you can see how Git organizes its internal data.
Terminal
ls -la my-project/.git
Expected OutputExpected
total 48 drwxr-xr-x 7 user user 4096 Jun 10 12:00 . drwxr-xr-x 3 user user 4096 Jun 10 12:00 .. drwxr-xr-x 2 user user 4096 Jun 10 12:00 branches drwxr-xr-x 2 user user 4096 Jun 10 12:00 hooks drwxr-xr-x 2 user user 4096 Jun 10 12:00 info drwxr-xr-x 4 user user 4096 Jun 10 12:00 objects drwxr-xr-x 4 user user 4096 Jun 10 12:00 refs -rw-r--r-- 1 user user 23 Jun 10 12:00 HEAD -rw-r--r-- 1 user user 73 Jun 10 12:00 config -rw-r--r-- 1 user user 56 Jun 10 12:00 description
This shows the current branch or commit that Git is pointing to, which is important for understanding what you are working on.
Terminal
cat my-project/.git/HEAD
Expected OutputExpected
ref: refs/heads/main
Key Concept

If you remember nothing else from this pattern, remember: the .git directory holds all the hidden data that makes Git track your project history and branches.

Common Mistakes
Deleting or modifying files inside the .git directory manually
This can corrupt your Git repository and cause loss of history or broken branches.
Use Git commands like git reset or git clean to safely modify repository state instead of touching .git files directly.
Ignoring the .git directory when copying or backing up a project
Without the .git folder, you lose all version history and Git tracking for the project.
Always include the .git directory when copying or backing up a Git repository to keep its full history.
Summary
The .git directory is a hidden folder that stores all Git data and history.
It contains folders like objects, refs, and files like HEAD and config that organize commits and branches.
Use Git commands to interact with this data safely instead of editing .git files manually.