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 initOutput:
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 main3. What to Do Immediately After git init
- Create a
.gitignorefile before staging any files:echo "node_modules/\n.env*\ndist/" > .gitignore - Stage your files:
git add . - Make your initial commit:
git commit -m "chore: initial commit"
Related Topics
Git Repositories & The .git Directory
Understanding Git repositories: local vs remote, the internal .git folder structure (objects, refs, HEAD, config), and bare repositories.
git clone — Cloning a Remote Repository
How to clone remote Git repositories: HTTPS vs SSH cloning, shallow clones (--depth 1), branch targeting (-b), and directory naming.