Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Learn the Basics  ›  Capstone Project

git init — Starting a Repository

git init 12 mingit init turns a folder into a repository by adding a hidden .git folder that stores all history, leaving the existing files untracked.

git init — Starting a Repository

Asha's recipe-box folder has three files and no history. One command turns it into a Git repository, and from that moment Git can start recording snapshots. It is also the command people most often run in the wrong folder.

The rule

git init adds a hidden .git folder; that folder is the repository.

Run it inside the project folder:

cd recipe-box
git init

Git replies with something like Initialized empty Git repository in /Users/asha/recipe-box/.git/. Your files are untouched. The new, hidden .git folder holds everything Git knows: every snapshot, every branch, and the repository's own settings.

That gives two parts to every project:

  • The working directory: the ordinary files you edit (README.md, pancakes.md, chai.md).
  • The repository: the .git folder with the history.

Straight after git init, the history is empty. The files are untracked: Git sees them but has not been told to record them yet.

git status
# On branch main
# No commits yet
# Untracked files:
#   README.md
#   chai.md
#   pancakes.md

Nothing is recorded until you ask. git init only prepares the storage; the next lessons show how files move from untracked to committed, one deliberate step at a time.

Some useful facts:

  • Want a new folder and a repository in one go? git init recipe-box creates the folder too.
  • Running git init again in the same folder is harmless; it does not wipe history.
  • Deleting the .git folder deletes the entire history. The files remain, but the project is no longer a repository.
  • On older Git versions the first branch may be called master. Set init.defaultBranch to main, as in the last lesson, or run git init -b main.

The shortcut

Before running git init, ask "Is this the top folder of one project?" The folder where you run it becomes the root of everything tracked beneath it.

Starting recipe-box the right wayStep 1 of 4
  1. Check where you are

    pwd prints /Users/asha/recipe-box. This is the project's top folder.

If you ever see thousands of untracked files you did not expect, you are probably inside a repository you created by accident. Find it with git rev-parse --show-toplevel, then remove that stray .git folder with care.

Drills

Answer fast — trust the rule.

quick drill

What does git init create?

quick drill

Right after git init, how does git status describe Asha's existing recipe files?

quick drill

What happens if you delete the .git folder of a project?

quick drill

Which command makes a new folder called recipe-box and initialises it as a repository?

Remember

  • git init adds a hidden .git folder; that folder is the repository.
  • Run it only at the top folder of one project.
  • Deleting .git deletes history, not files; running git init twice is harmless.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhat just happened?

Asha runs git init inside recipe-box. Explain what changed on disk, what git status will show for her three recipe files, and what would happen if she later deleted the .git folder.