Initializing a Repository (git init and .git Directory Anatomy)0%
Tracking Changes: git status and Staging with git add

Initializing a Repository (git init and .git Directory Anatomy)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Initializing a Repository (git init and .git Directory Anatomy)

Every Git project begins with repository initialization. Understanding what actually happens when you initialize a repository demystifies Git's internals and gives you total confidence when diagnosing repository issues.


1. Initializing a Repository with git init

To transform an existing project folder into a Git repository, navigate to the folder in your terminal and execute:

Bash / Terminal
# Navigate to your project directory
cd my-web-app
 
# Initialize the Git repository
git init

Terminal Output:

Output
Initialized empty Git repository in D:/projects/my-web-app/.git/
Note
[!NOTE] Running git init in a directory that already has a .git folder will not overwrite your existing commits or history. It simply re-initializes configuration or updates templates safely.

2. Deep Dive: Anatomy of the .git Directory

When you run git init, Git creates a hidden directory named .git. This hidden folder contains the entire database, configuration, and state of your repository.

Let's inspect what lies inside .git:

Output
.git/
├── HEAD # Text file pointing to currently checked-out branch (e.g., ref: refs/heads/main)
├── config # Project-specific configuration options (--local)
├── description # Used primarily by GitWeb (default description text)
├── hooks/ # Client-side and server-side script hooks (pre-commit, pre-push, etc.)
├── info/
│ └── exclude # Local exclusion file (similar to .gitignore, but not shared with others)
├── objects/ # Object Database: stores all blobs (files), trees (directories), and commits
│ ├── info/
│ └── pack/ # Compressed packfiles for network and disk efficiency
└── refs/ # References pointers
├── heads/ # Pointers to local branches (e.g., main, feature-auth)
└── tags/ # Pointers to release tags (e.g., v1.0.0)

The 4 Key Components You Must Know:

  1. 1
    HEAD:
  • A single-line text file containing a reference pointer: ref: refs/heads/main.
  • It tells Git which branch and which commit your working directory currently reflects.
  1. 1
    config:
  • Stores repository-level preferences, remote repository URLs (origin), and branch tracking configurations.
  1. 1
    objects/:
  • The heart of Git's content-addressable storage. Files are compressed with zlib and stored by their 40-character SHA hash.
  1. 1
    refs/heads/:
  • Contains small files named after your branches. Each file contains nothing more than a 40-character commit hash.

3. Creating a Repo with a Custom Initial Branch

Modern development best practices specify main as the default branch rather than master:

Bash / Terminal
# Initialize repository with initial branch explicitly named 'main'
git init -b main

4. How to Delete a Git Repository

What if you initialized Git in the wrong folder (e.g., accidentally inside your entire Desktop or Home directory)?

  • To remove Git version control, you do not need to uninstall Git.
  • Simply delete the hidden .git folder! Your project source files will remain completely untouched, but all version control history will be deleted.
Bash / Terminal
# On Linux / macOS:
rm -rf .git
 
# On Windows PowerShell:
Remove-Item -Recurse -Force .git

Interactive Quiz

Practice Quiz

Question 1
What command initializes a brand new Git repository in the current working directory?
Question 2
What is the name of the hidden directory created by git init that stores all version control history?
Question 3
What is the role of the HEAD file inside the .git directory?
Question 4
How can you completely remove Git tracking from a folder while keeping all your actual project code files intact?
Question 5
What is stored inside the .git/objects/ folder?
Next Lesson

Tracking Changes: git status and Staging with git add

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext