Git Init

The git init command creates a brand new Git repository. It is always the very first Git command used when starting a new project. It tells Git to start tracking all changes in the specified folder.

When git init runs, Git creates a hidden folder called .git inside the project directory. This .git folder is the engine of Git — it stores all the history, branches, and configuration for the project.

Syntax

git init

Or to initialize a new folder with a specific name:

git init folder-name

Example 1 — Initializing Git in an Existing Folder

Suppose there is a project folder called my-website. Navigate into it and initialize Git:

# Step 1: Go into the project folder
cd my-website

# Step 2: Initialize Git
git init

Output:

Initialized empty Git repository in /home/ravi/my-website/.git/

Git is now tracking the my-website folder.

Example 2 — Creating a New Project Folder with Git

To create a new folder and initialize Git in it at the same time:

git init my-blog

Output:

Initialized empty Git repository in /home/ravi/my-blog/.git/

This creates the my-blog folder and sets up Git inside it in one step.

What Gets Created — The .git Folder

After running git init, the following structure is created inside the project folder:

my-website/
├── .git/               ← Hidden folder (Git's database)
│   ├── config          ← Repository-level configuration
│   ├── HEAD            ← Points to current branch
│   ├── objects/        ← Stores all commits and file contents
│   ├── refs/           ← Stores branch and tag pointers
│   └── hooks/          ← Scripts that run on Git events
├── index.html          ← (existing project files)
└── style.css

Important: The .git folder is hidden by default. On Linux/macOS, use ls -a to see it. On Windows, enable "Show hidden items" in File Explorer.

Verifying Git is Initialized

To confirm that Git has been successfully initialized, run:

git status

If Git is initialized, the output will be:

On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

If Git is NOT initialized, the output will be:

fatal: not a git repository (or any of the parent directories): .git

Initializing Git in the Wrong Folder — What to Do

If git init is accidentally run in the wrong folder, the .git folder can simply be deleted to undo the initialization:

On Linux/macOS:

rm -rf .git

On Windows (Command Prompt):

rmdir /s /q .git

After deleting the .git folder, the directory is no longer a Git repository.

git init with a Bare Repository

A bare repository is a special type of repository that has no working directory — it only stores Git history. This is typically used for remote/server repositories (like what GitHub uses internally).

git init --bare project.git

Bare repositories are for server-side use, not for everyday local development.

Re-initializing an Existing Repository

If git init is run inside a folder that is already a Git repository, it will simply re-initialize it without deleting any existing history. This is safe to do.

git init

Output:

Reinitialized existing Git repository in /home/ravi/my-website/.git/

Common Mistakes

MistakeSolution
Running git init in the home directory instead of the project folderDelete the .git folder from the home directory, then cd into the correct folder first
Running git init inside an existing Git repo (nested repos)Avoid nesting Git repos unless using submodules intentionally
Deleting the .git folder accidentallyAll Git history will be lost — always keep backups or push to a remote

Summary

git init is the starting point of every Git project. It creates a hidden .git folder that stores all the history and tracking data. Run it once per project, from inside the project folder. After initialization, use git status to confirm that Git is active and ready to track changes.

Leave a Comment

Your email address will not be published. Required fields are marked *