Git Initialize (git init)
What is Git Initialize?
Git Initialize (git init) is the command used to create a new Git repository in a project folder.
It tells Git to start tracking changes in that directory.
When you run the command, Git creates a hidden folder called:
.git
This .git folder stores all Git information such as:
- commit history
- branches
- configuration
- version tracking metadata
Once initialized, the folder becomes a Git repository, and you can start using Git commands like:
git addgit commitgit pushgit pull
Real Life Example
Imagine you are starting a Laravel project.
Initially, it is just a normal folder.
laravel-project/
app/
routes/
resources/
composer.json
Git does not track anything yet.
When you run:
git init
Git converts this folder into a version-controlled repository.
Now every file change can be tracked.
Basic Syntax
git init
This initializes the current directory as a Git repository.
Step-by-Step Example
Step 1: Create a Project Folder
mkdir my-project
cd my-project
Folder structure:
my-project/
Step 2: Initialize Git Repository
Run:
git init
Output:
Initialized empty Git repository in /my-project/.git/
Now the structure becomes:
my-project/
.git/
The .git folder contains Git configuration and history.
Step 3: Create Files
Create a file:
index.html
Example:
<h1>Hello Git</h1>
Step 4: Check Git Status
Run:
git status
Output:
Untracked files:
index.html
This means Git sees the file but is not tracking it yet.
Step 5: Add File to Git
git add index.html
or add all files
git add .
Step 6: Commit the File
git commit -m "Initial commit"
Now Git saves the first version of your project.
How Git Works After Initialization
After running git init, Git tracks changes in three stages.
Working Directory
Your project files.
Example
index.html
style.css
Staging Area
Files prepared for commit.
Command:
git add file_name
Repository
Final saved version in Git history.
Command:
git commit
Flow:
Working Directory → Staging Area → Git Repository
Example Workflow
git init
git add .
git commit -m "First commit"
This workflow:
- Creates repository
- Adds files
- Saves project snapshot
Important Notes About git init
1. It Creates a Hidden Folder
Git creates:
.git
This folder stores:
- commit history
- branch information
- configuration
Never delete .git unless you want to remove version control.
2. Git Init Works Locally
git init only creates a local repository.
To connect to GitHub later:
git remote add origin repository_url
Example:
git remote add origin https://github.com/user/project.git
3. You Run It Only Once
You normally run:
git init
only once per project.
Example in a Laravel Project
For example, if you start a Laravel project:
composer create-project laravel/laravel blog
cd blog
Then initialize Git:
git init
git add .
git commit -m "Initial Laravel setup"
Now the entire Laravel codebase is tracked.
How to Check if Git is Initialized
Run:
git status
If initialized, you will see something like:
On branch main
nothing to commit, working tree clean
Common Mistake Beginners Make
Running Git commands without initializing
Example:
git add .
Error:
fatal: not a git repository
Solution:
git init
When Should You Use git init?
Use git init when:
- Starting a new project
- Converting an existing folder to Git
- Beginning version control
Example scenarios:
✔ Laravel project ✔ React project ✔ Node.js project ✔ Static website
Git Init vs Git Clone
| Command | Purpose |
|---|---|
| git init | Create new repository |
| git clone | Download existing repository |
Example:
git clone https://github.com/user/project.git
Best Practices
✔ Always initialize Git at the start of a project
✔ Add .gitignore file
✔ Make meaningful commits
✔ Commit frequently
Example .gitignore for Laravel:
vendor/
node_modules/
.env
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
