Skip to main content
⚡ OpenDevDocs
Git
gitgitGuideBeginner

git init — Initializing a New Repository

How to initialize a brand new Git repository: creating the .git directory, specifying initial branch names, and best practices.

Verified: 2026-09-30
Platforms:
all
#git#init#repository#commands

git init — Initializing a New Repository

The git init command creates a new, empty Git repository or reinitializes an existing one. It creates the hidden .git directory containing all necessary metadata.


1. Syntax and Basic Usage

# 1. Navigate to your project root folder:
cd /path/to/my-project

# 2. Initialize Git:
git init

Output:

Initialized empty Git repository in /path/to/my-project/.git/

2. Specifying the Default Branch Name

To guarantee the primary branch is named main (the modern standard) rather than legacy master:

git init --initial-branch=main
# or short flag:
git init -b main

3. What to Do Immediately After git init

  1. Create a .gitignore file before staging any files:
    echo "node_modules/\n.env*\ndist/" > .gitignore
  2. Stage your files:
    git add .
  3. Make your initial commit:
    git commit -m "chore: initial commit"

On this page