Git & GitHub Course
Git & GitHub
/
Beginner

git init

Definition

The command used to create a new, empty Git repository or reinitialize an existing one.

Explain Like I'm New

You have a folder on your computer full of code. Git is currently ignoring it. By typing `git init`, you awaken Git. It creates a secret hidden folder called `.git` that acts as the 'brain' and starts tracking your files.

Real World Example

Starting a brand new web project from scratch on your local computer.

Common Use Cases

  • •Starting new local projects

`git init` Options

FlagDescription
`git init`Initializes a standard local git repository in the current folder (creates `.git`).
`--bare`Creates a bare repository without a working directory (used for centralized servers/remotes).
`-b [branch_name]`Sets the initial branch name (e.g., `main` instead of `master`) upon creation.

Terminal Output

bash / terminal
# Create a new folder for your project $ mkdir my-awesome-app $ cd my-awesome-app # Tell Git to start tracking this folder $ git init # Output: Initialized empty Git repository in /path/to/my-awesome-app/.git/ # If you run 'ls -a' (list all files, including hidden ones), # you will see the secret .git folder! $ ls -a # Output: . .. .git

Interview Questions

basic

  • What does `git init` actually do under the hood?

intermediate

  • If you delete the `.git` folder, what happens to your code?

Flash Cards

Question

What does it do?

Click to reveal answer
Answer

It creates a hidden directory named `.git` inside your project folder. This folder contains all the necessary metadata and object databases for version control.

Question

Delete .git folder?

Click to reveal answer
Answer

Your code (HTML, CSS, JS files) remains perfectly intact! But your entire Git history (all past saves, branches, and commits) is permanently deleted. It just becomes a normal, untracked Windows/Mac folder again.