The git init Command
The git init command creates a new Git repository. It's the first command you run when starting a new project with Git.
What Does git init Do?
When you run git init, Git creates a hidden .git directory in your project folder. This directory contains all the information Git needs to track your project.
1# Initialize a new repository
2git init
3
4# Output:
5Initialized empty Git repository in /path/to/project/.git/The .git Directory
After running git init, your project structure looks like this:
my-project/
├── .git/ ← Created by git init
│ ├── HEAD ← Points to current branch
│ ├── config ← Repository configuration
│ ├── description ← Repository description
│ ├── hooks/ ← Git hooks scripts
│ ├── info/ ← Exclude patterns
│ ├── objects/ ← Object database
│ └── refs/ ← Branch and tag references
├── index.html
├── style.css
└── app.js
Basic Usage
Initialize a New Project
1# Create a new project folder
2mkdir my-project
3cd my-project
4
5# Initialize Git
6git init
7
8# Check status
9git status
10# Output: On branch main, No commits yetInitialize an Existing Project
1# Navigate to existing project
2cd existing-project
3
4# Initialize Git
5git init
6
7# Your files are now untracked
8git status
9# Output: Untracked files: index.html, style.css, app.jsOptions
Create with a Specific Branch Name
1# Initialize with 'main' as default branch
2git init --initial-branch=main
3
4# Or use the shorter form
5git init -b mainInitialize a Bare Repository
A bare repository is used as a central repository (like on a server):
1# Create a bare repository (no working directory)
2git init --bare my-project.gitQuiet Mode
1# Initialize without output message
2git init --quiet
3# or
4git init -qAfter Initialization
Once initialized, you typically:
1. Create a .gitignore File
1# Create .gitignore
2echo "node_modules/" > .gitignore
3echo ".env" >> .gitignore2. Stage Your Files
1# Stage all files
2git add .
3
4# Or stage specific files
5git add index.html style.css3. Make Your First Commit
1git commit -m "Initial commit"4. Connect to Remote
1git remote add origin https://github.com/username/repo.git
2git push -u origin mainCommon Scenarios
Starting a Brand New Project
1mkdir my-new-project
2cd my-new-project
3git init
4echo "# My Project" > README.md
5git add README.md
6git commit -m "Initial commit"Adding Git to an Existing Project
1cd existing-project
2git init
3git add .
4git commit -m "Initial commit: Add existing project files"Reinitializing a Repository
Running git init in an existing repository is safe:
1# This won't overwrite existing configuration
2git init
3# Output: Reinitialized existing Git repository in /path/.git/What Happens Behind the Scenes
When you run git init:
- Git creates the
.gitdirectory - Sets up the object database for storing commits
- Creates refs directories for branches and tags
- Sets the default branch (usually
mainormaster) - Creates configuration files
Important Notes
Don't Edit .git Manually
The .git directory is managed by Git. Avoid editing files inside it unless you know what you're doing.
Repository Within Repository
Don't initialize a Git repository inside another Git repository:
1# Bad: Nested repositories cause issues
2cd my-project
3git init
4mkdir subproject
5cd subproject
6git init # ❌ Don't do this!Use Git submodules instead if you need nested repositories.
Summary
The git init command:
- Creates a new Git repository
- Sets up the
.gitdirectory - Prepares your project for version control
- Is safe to run on existing repositories
You're now ready to practice with the init command!
