git init

Setup & Creating Projects

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>]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (1)

Name of the first branch (otherwise init.defaultBranch, or master).

RecommendedPick the first branch name explicitly (e.g. main)

Example

git init -b main

Choosing the Right Option

init vs clone

Two ways to get a repository

CommandStarts withUse when
git initAn empty repositoryYou start a new project
git clone <url>A full copy of a remote repositoryThe project already exists

Examples

Start a repository here

git init

Creates .git in the current folder.


Choose the first branch name

git init -b main

The first branch is called main.


In a new folder

git init shop

Creates the shop folder with a repository inside.

Workflow Examples

1
git init -b main

Create the repository

2
git add . && git commit -m "Initial commit"

First commit

3
git remote add origin git@github.com:you/shop.git

Link the empty GitHub repository

4
git push -u origin main

Publish

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

A:

Delete the hidden .git folder. Your files stay.

A:

Yes. It reports "Reinitialized existing Git repository" and keeps everything.