Git & GitHub ยท Chapter 4 of 42

git init

The `git init` command creates a new, empty Git repository in the current folder. It adds a hidden `.git` directory that stores all the history, branches, and configuration for that project.

You only run `git init` once per project, when you're starting version control from scratch. If you're joining an existing project hosted online, you would use `git clone` instead.

Syntax
git init

Creating a repository

Navigate to your project folder and run `git init`. This turns an ordinary folder into a Git repository able to track changes.

The .git folder

git init creates a hidden `.git` folder containing all repository data. Deleting this folder removes all Git history but leaves your files untouched.

Example 1 (bash)
mkdir my-project
cd my-project
git init
Output
Initialized empty Git repository in /my-project/.git/

Creates a new folder and initializes it as a Git repository.

Example 2 (bash)
git init existing-folder
Output
Initialized empty Git repository in existing-folder/.git/

Initializes Git inside an existing folder by passing its name as an argument.

Key points

  • git init creates a new local Git repository.
  • It adds a hidden .git folder that stores all history.
  • You run it once per project when starting from scratch.
  • Use git clone instead when copying an existing remote project.
๐Ÿ’ก Note: Never manually edit files inside the .git folder โ€” let Git commands manage it.

๐Ÿ“ Quick Quiz

1. What does git init do?

2. Where does Git store repository data?

3. When should you use git clone instead of git init?