← Back to Git Course | Chapter 2: Basic Commands | Lesson 1 of 18

Git init Command

What is git init?

git init is the single command that turns any ordinary folder into an active Git repository -- it creates the hidden .git directory containing the object database, refs, and configuration Git needs to start tracking changes.

Example: What is git init?

bash
git init

Initializing an Empty Repository

You can also run git init <folder-name> to create a brand-new directory and initialize it as a repository in one step, which is handy when starting a project from scratch rather than converting an existing folder.

Example: Initializing an Empty Repository

bash
git init my-new-project

Custom Default Branch

New repositories need a starting branch name, and modern Git lets you choose it explicitly with git init -b main (or set a global default via init.defaultBranch) instead of accepting whatever the installed default happens to be.

Example: Custom Default Branch

bash
git init -b main

Re-initializing a Repository

Running git init again inside a folder that's already a repository is completely safe -- Git will not touch your existing commits or files, it only re-creates any missing configuration files, which is occasionally useful for repairing a corrupted setup.

Example: Re-initializing a Repository

bash
git init

Initializing a Bare Repository

A bare repository, created with git init --bare, has no working directory of checked-out files at all -- it holds only the raw tracking data, which is exactly what you want on a server acting purely as a central hub that others push to and pull from.

Example: Initializing a Bare Repository

bash
git init --bare

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.