# Initializing a Repository

> How to turn a normal folder into a Git repository using `git init`.

Subject: Version Control | Lesson 3 of 29 | 3 min read
Interactive lesson: https://codewithanimation.in/git/git-init
Previous: [The Three Trees Architecture](https://codewithanimation.in/git/the-three-trees.md) | Next: [Tracking Changes](https://codewithanimation.in/git/git-add-and-status.md)

## git init

The `git init` command is how you start using Git in an existing project. It transforms a standard folder on your computer into a Git repository.

You only ever need to run this command once per project.

## Why it matters

Before you run `git init`, Git has no idea your folder exists. You can't track changes, create branches, or commit code.

Running this command is the required first step before you can use any other Git features locally.

## How it works under the hood

When you run `git init`, Git doesn't change any of your project's files. Instead, it creates a single, hidden folder called `.git` at the root of your project.

This `.git` folder is the actual 'Repository' (Tree #3 from the Three Trees). It contains all the internal plumbing Git uses to store your snapshots, branches, and config. If you delete the `.git` folder, the folder stops being a repository entirely, but your actual code files remain intact.

## Common mistake: Running git init in the wrong folder

The #1 mistake beginners make is running `git init` in their root User folder (e.g., `~` or `C:/Users/Name`). This accidentally turns your entire computer into one massive Git repository, tracking every single file you own. Always ensure you have `cd`'d into your specific project folder first.

## Tip: Checking your status first

If you aren't sure if a folder is already a Git repository, run `git status`. If it isn't, Git will explicitly tell you: `fatal: not a git repository`. If you see that, it's safe to `git init`.

## Try it: See what git init creates

Inspect the repository database beside the project files that Git leaves unchanged.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-init

## Check yourself

### Question 1

What exactly does the `git init` command do to your project files?

- A. It formats your code and removes unused variables.
- B. It connects your local folder to a GitHub repository.
- C. It creates a hidden `.git` folder without touching your actual project files.
- D. It creates an initial backup copy of all your files.

Correct answer: C

`git init` just creates the hidden `.git` folder (the repository infrastructure). It doesn't modify your existing files or save them yet.

- Why not A: Git doesn't care about the contents of your code.
- Why not B: That requires `git remote add` and an internet connection. `git init` is purely local.
- Why not D: You must run `git add` and `git commit` to create your first backup/snapshot.

### Question 2

How many times do you need to run `git init` in a single project?

- A. Every time you open your terminal.
- B. Only once.
- C. Every time you want to make a new commit.
- D. Before every file you create.

Correct answer: B

You only run `git init` once at the very beginning to establish the repository. After that, Git is permanently active in that folder.

- Why not A: The `.git` folder persists between terminal sessions.
- Why not C: You use `git commit` for that.
- Why not D: Git tracks the whole folder once initialized.

### Question 3

What happens if you delete the hidden `.git` folder?

- A. Your code is deleted.
- B. The folder is no longer a Git repository, and all version history is lost.
- C. Nothing, Git automatically regenerates it.
- D. Your local code is synced with the remote repository.

Correct answer: B

The `.git` folder *is* the repository. If you delete it, you lose all your commits, branches, and Git settings, but your Working Directory (your actual project files) is completely untouched.

- Why not A: The `.git` folder only stores the history, not the current working files.
- Why not C: Git cannot regenerate lost history.
- Why not D: Deleting the `.git` folder actually removes all remote configurations.

## Remember this

- Run `git init` once per project.
- It creates the hidden `.git` directory.
- Never run it in your home directory.
