You type git status, and the terminal answers fatal: not a git repository. The word fatal sounds scary, but it means something simple: Git doesn't know this folder yet. Nobody has told Git to keep records here.
The two commands in this lesson fix that. git init turns a normal folder into a Git repository (a folder whose history Git records). git status tells you the state of every file in it. Say you've just moved into a new rented room and started an inventory book, a list of your things. On day one the book is empty, so everything in the room is "not recorded yet". In Git, the room is your project folder, your things are the files, and the inventory book is Git's records. git init opens that empty book, and git status compares what's in the room with what's in the book.
git init: the folder becomes a repository
The project folder in this example holds two files, index.html and style.css, and Git doesn't know it yet. The example below runs git status, then git init:
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ git init
Initialized empty Git repository in /project/.git/
fatal: not a git repositorymeans Git looked for a.gitfolder in this folder, then in every folder above it (the folder that holds this one, and so on up), and found none.git initcreates a hidden.gitfolder. That's where Git keeps all its data: commits (saved versions), branches (separate lines of work) and the repo's settings. Your own files aren't changed or moved.- A folder whose name starts with a dot is hidden. In the terminal, the command
ls -alists what's in a folder, hidden items included.
In the exercise, the Git Simulator calls the first branch main. The Git on your computer may call it master and print advice about that name (lines that start with hint). To make every new repository on your computer use main too, run git config --global init.defaultBranch main once. In a repository that already uses master, run git branch -m main to rename that branch to main.
git status: comparing the folder with Git's records
Here is what git status answers right after git init:
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
style.css
nothing added to commit but untracked files present (use "git add" to track)
On branch main: you're on themainbranch.No commits yet: there isn't a single commit yet.Untracked files: files that are in the folder, but that Git has never recorded. Both example files are here.- The last line means: no file has been picked for a commit yet, but there are untracked files.
- The text in brackets suggests the next command. Git often gives hints like this, so get used to reading them.
Once you start picking files and making commits, git status shows other parts too. These are the four you'll meet most often:
Part of git status |
What it means |
|---|---|
Untracked files |
new files, never recorded by Git |
Changes to be committed |
picked with git add, they go into the next commit |
Changes not staged for commit |
recorded before, then changed, but not picked again yet |
nothing to commit, working tree clean |
every file matches the last commit |
Staged means picked for the next commit. Working tree means the files in your project folder. You'll learn git add and git commit in the next lesson. git status itself only reads and changes nothing, so it's safe to run as often as you like.
Running git init again
A lot of people are afraid to run git init in a folder that's already a repository. They worry that it deletes the commit history. Take a guess, then look at Git's answer:
$ git init
Reinitialized existing Git repository in /project/.git/
Git only checks the existing .git folder again. Your commits, branches and files are all still there: nothing is deleted, and there's no second repo.
📌 The short version: git status -s
-s is an option (often called a flag): an extra part after a command that changes what it does. With -s (or its long form, --short), git status prints one line per file:
$ git status -s
?? index.html
?? style.css
?? means untracked. Later you'll also see A (a new file that has been picked with git add) and M (a changed file).
⚠️ What often goes wrong when a repo starts
git initin the wrong folder. Run it in the project folder, not in your home folder (your account's main folder) or on the Desktop. In the wrong place,git statuslists hundreds of untracked files. First check where you are withpwd(a command that prints the folder your terminal is in).- A repo inside a repo. Running
git initagain in a subfolder (a folder inside a folder) of a repository makes a separate repository inside it. Then the outer repository stops storing that subfolder's contents the normal way. Onegit initin the project's top folder is enough. - Deleting the
.gitfolder. The whole history is gone, and the folder is a normal folder again. - Thinking
git statussaves something.git statusonly shows. Nothing is saved until you make a commit.
TL;DR: git init creates the hidden .git folder and makes your project folder a repository (a folder whose history Git records). Running it again is safe: Git answers Reinitialized existing Git repository and deletes nothing. git status compares the folder with Git's records. Files Git has never recorded are under Untracked files. Files picked with git add are under Changes to be committed. Files recorded before, then changed but not picked again, are under Changes not staged for commit. When every file matches the last commit (saved version), the answer is nothing to commit, working tree clean. git status -s prints the short version, one line per file. git status only reads, so run it as often as you like.
⚡ The folder becomes a repository
Write:
git init
Meaning: git init creates the hidden .git folder. From then on your folder is a repository (a folder whose history Git records). In .git, Git keeps commits (saved versions), branches (separate lines of work) and the repo's settings. Your own files aren't changed. Before git init, the command git status answers fatal: not a git repository: Git doesn't know this folder yet.
⚡ Compare the folder with Git's records
Write:
git status
Meaning: git status shows the state of every file. Right after git init, every file is under Untracked files: it's in the folder, but Git has never recorded it. git status only reads, so run it as often as you like.
⚡ git init twice
Guess first: you run git init in a folder that's already a repository. Is the history deleted?
No. Git answers Reinitialized existing Git repository and only checks the existing .git folder again. Your commits, branches and files are all still there, and no second repo appears.
Meaning: Running git init again in the same folder is safe. What to avoid is git init in a subfolder (a folder inside the repository), because that makes a separate repository inside it.