FIX · ERROR

fatal: not a
git repository.

Git looked in this folder and every folder above it, and found no repository. Nothing is broken. You are in the wrong place, or the repository was never created here.

pwdls -agit initgit status

The exact message

what you see

A repository is a folder that has a hidden .git folder inside it. Git walks up from where you are, one folder at a time, looking for that .git folder. The message means it reached the top of the disk and found none. The same message appears for git add, git commit, git log and most other commands.

What Git is looking for

a picture
✓

A repository

my-project/
  .git/   <- Git looks for this
  index.html
  notes.txt
✗

Not a repository

my-project/
  index.html
  notes.txt
  (no .git anywhere above)
?

Wrong folder

Downloads/   <- you are here
  my-project/
    .git/

Step 1: find out where you are

10 seconds
1

Where am I?

Run pwd (macOS, Linux, Git Bash) or cd on its own (Windows cmd). It prints the folder you are in.

2

What is in here?

Run ls -a (dir /a on Windows cmd). The -a shows hidden items. Look for a line that says .git.

3

Is it above me?

No .git here? Run cd .. to go up one folder and look again. Git does this walk for you, so if it still fails, none of the parents have one either.

Step 2: match your case

one of three
what you see
what it means
the fix
Your files are here, but ls -a shows no .git, and you never ran git init or git clone
This folder was never a repository
git init turns the current folder into one. Then git status works.
pwd shows a folder like Downloads, Desktop or your home folder, not your project
You opened the terminal in the wrong place
cd into the project folder, then try again. Tip: in Chapter 3 you saw how to open a terminal inside a folder.
This used to be a repository, and now ls -a shows no .git
The .git folder was deleted or moved. It holds all your history.
If you have a copy elsewhere (a clone, a GitHub repo, a backup), use that. Running git init here only starts an empty history. Your old commits do not come back.

Check it worked: after git init, git status prints On branch ... and No commits yet. The default branch name depends on your Git settings (Chapter 5 sets it to main).

See it happen

a real run

The branch name and the path in your own output will differ. The point: the same command that failed works right after git init.

Look-alike mistakes

same message, different cause
fatal: not a git repository (or any of the parent directories): .gitYou are inside the .git folder or a siblingRun cd .. until you are in the project folder that contains .git, not inside it.
warning: Not a git repository. Use --no-index to compare two paths outside a working treeYou ran git diff outside a repositorygit diff is the one command that words it as a warning. The cause and the fix are the same as above.
fatal: ambiguous argument 'HEAD~5': unknown revision or path not in the working tree.Different problem: you went further back than your historyThis one means you are in a repository. Run git log --oneline and count your commits first.

Questions people ask

What does "not a git repository" mean?

Git could not find a .git folder in the current folder or in any folder above it. Without that folder Git has no history to work with.

Do I lose my files if I see this error?

No. The error only says Git has no repository to look at. Your files are untouched.

Should I run git init every time I see it?

Only if the folder was never a repository. If you are in the wrong folder, cd to the right one. If a .git folder was deleted, git init starts a new empty history and does not restore the old one.

Why does ls not show the .git folder?

It is a hidden folder. Use ls -a on macOS, Linux or Git Bash, or dir /a on Windows cmd.

Git, Visually · Parts 1 and 2 · English