git init
Create an empty Git repository
The 'git init' command turns the current folder into a Git repository by creating a hidden .git folder. The repository starts empty: your files are untracked until you add and commit them. Running it again in an existing repository is safe — it changes nothing. Use it for a brand-new project; to work on an existing one, use git clone instead.
Synopsis
git init [-q | --quiet] [-b <branch-name> | --initial-branch=<branch-name>] [<directory>]Options
Choosing the Right Option
init vs clone
Two ways to get a repository
| Command | Starts with | Use when |
|---|---|---|
git init | An empty repository | You start a new project |
git clone <url> | A full copy of a remote repository | The project already exists |
Examples
Start a repository here
git initCreates .git in the current folder.
Choose the first branch name
git init -b mainThe first branch is called main.
In a new folder
git init shopCreates the shop folder with a repository inside.
Workflow Examples
git init -b mainCreate the repository
git add . && git commit -m "Initial commit"First commit
git remote add origin git@github.com:you/shop.gitLink the empty GitHub repository
git push -u origin mainPublish
Troubleshooting
fatal: not a git repository
Cause: You are outside any repository
cd into the project, or run git init there
The first branch is called master
Cause: init.defaultBranch is not set
git init -b main, or git config --global init.defaultBranch main
Tips & Pitfalls
Best Practices
- ✓Add a .gitignore before the first commit.
- ✓Set init.defaultBranch once globally.
Common Mistakes
- ✗Running git init inside an existing repository folder and nesting repositories.
- ✗Running git init in your home folder and tracking your whole computer.
- ✗Using git init + git remote add when git clone would copy the project directly.
FAQ
Delete the hidden .git folder. Your files stay.
Yes. It reports "Reinitialized existing Git repository" and keeps everything.