git init
Creating a new Git repository with git init and what happens behind the scenes.
What you'll learn
- How to initialize a new Git repository
- What git init creates in the .git folder
- How to reinitialize or check an existing repo
- The default branch name and how to change it
Concept
What git init Does
git init turns an ordinary directory into a Git repository. It creates the hidden .git subdirectory and populates it with the structures Git needs to start tracking changes.
mkdir my-project
cd my-project
git init
After running this, the directory is a Git repository — but it is empty. No files are being tracked yet. The next steps are usually to create a file, git add it, and git commit.
Behind the Scenes
When you run git init, Git creates:
objects/— where file contents and commits are stored as compressed objectsrefs/heads/— where branch pointers live (initially empty)refs/tags/— where tag pointers liveHEAD— a file that points to the current branch (initiallyrefs/heads/mainorrefs/heads/master)config— repository-specific configurationdescription— a placeholder file (rarely used)
Nothing is committed yet — the repository has zero commits and no branches until you make your first commit.
The Default Branch Name
Historically, Git's default branch was called master. Since 2020, the community has moved toward using main as the default, and Git itself changed its default in version 2.28.
You can set the default branch name globally:
git config --global init.defaultBranch main
If your repository was created with master and you want to rename it:
git branch -m main
Reinitializing
Running git init in an existing repository is safe — it does not overwrite anything, just makes sure the .git structure is complete. You might do this after manually copying a .git folder or if something got corrupted.
Cloning Instead of Initializing
If you want to work on an existing repository (for example, an open-source project on GitHub), use git clone instead of git init:
git clone https://github.com/user/repo.git
This downloads the entire repository, including all history, and sets up the remote connection automatically.
Example
# Start a brand new project
mkdir weather-app
cd weather-app
# Initialize Git — this creates the .git folder
git init
# Output: "Initialized empty Git repository in .../weather-app/.git/"
# Set the default branch name (do this once globally for all new repos)
git config --global init.defaultBranch main
# Create an initial file
echo "# Weather App" > README.md
# Stage and commit the first file
git add README.md
git commit -m "Initial commit: add README"
# See the result — you now have a repository with one commit
git log --oneline
git status
# If you initialized with the wrong branch name, rename it
git branch -m main
The complete first-run workflow: create a directory, initialize Git, set the default branch, make the first commit, and verify the result. This is how every new Git project begins.
Try it
- .gitignore Generator
Generate a .gitignore to accompany your new repository.
- Env File Generator
Create .env files — which you should gitignore, never commit.
Common mistakes
The mistake
Running git init inside an already-initialized repository
The fix
git init is safe to run again, but it is usually a sign you are in the wrong directory. Check for an existing .git folder first with ls -a. If you see .git, the repo is already initialized.
The mistake
Initializing Git in your home directory or root folder
The fix
Always cd into your specific project directory before running git init. Initializing in your home directory would track every file on your computer, which is catastrophic.
Related Resources
Related Cheatsheets
- Git Cheatsheet
git init and repository setup commands.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.