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

Git init Command

git init is like putting a brand new notebook inside an ordinary folder and saying 'start tracking everything here'. The folder becomes a place where Git watches your work.
Syntax
bash
git init [directory_name]
git init -b branch_name

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running git init inside a folder that is already a Git repository, or in a parent folder, so the project structure is tracked incorrectly.
  2. Expecting git init to create the first commit or the files, when it only creates the empty .git folder.
  3. Being surprised that the branch is named master on older setups, when you can choose the name with git init -b main.

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.