Skip
Arish's avatar

9. Introduction to the init Command


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.

bash
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

bash
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 yet

Initialize an Existing Project

bash
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.js

Options

Create with a Specific Branch Name

bash
1# Initialize with 'main' as default branch
2git init --initial-branch=main
3
4# Or use the shorter form
5git init -b main

Initialize a Bare Repository

A bare repository is used as a central repository (like on a server):

bash
1# Create a bare repository (no working directory)
2git init --bare my-project.git

Quiet Mode

bash
1# Initialize without output message
2git init --quiet
3# or
4git init -q

After Initialization

Once initialized, you typically:

1. Create a .gitignore File

bash
1# Create .gitignore
2echo "node_modules/" > .gitignore
3echo ".env" >> .gitignore

2. Stage Your Files

bash
1# Stage all files
2git add .
3
4# Or stage specific files
5git add index.html style.css

3. Make Your First Commit

bash
1git commit -m "Initial commit"

4. Connect to Remote

bash
1git remote add origin https://github.com/username/repo.git
2git push -u origin main

Common Scenarios

Starting a Brand New Project

bash
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

bash
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:

bash
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:

  1. Git creates the .git directory
  2. Sets up the object database for storing commits
  3. Creates refs directories for branches and tags
  4. Sets the default branch (usually main or master)
  5. 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:

bash
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 .git directory
  • Prepares your project for version control
  • Is safe to run on existing repositories

You're now ready to practice with the init command!