Fixing the Git “not a git repository” error
If git status or git commit prints fatal: not a git repository (or any of the parent directories): .git, the folder you are in is not inside a Git repository. Either cd into the folder that contains the .git folder (or a folder below it), or run git init to create a repository if you are starting to manage the files with Git. Either way, the error goes away.
This article covers the cause and the fix for cases where git add or git push stops with this error right after you git clone a project from GitHub. I reproduced the error in a test environment and included the messages exactly as they were printed. I also confirmed that the error disappears after the fix.
The test environment was Ubuntu 24.04 with Git 2.43.0. The messages were captured with English settings (LANG not set). I did not test macOS, Windows (Git for Windows), Japanese-translated messages, or GitHub Desktop. In the output shown in this article, I replaced the test folder paths with /path/to/…. I performed the folder changes with a test script, and the article writes them as cd. The article shows commands one at a time, but in some tests I ran them chained with &&.
The message you get
When I ran git status in an empty folder, it printed this single line and stopped. The exit code was 128.
$ ls -A
$ git status
fatal: not a git repository (or any of the parent directories): .git
git log, git add ., git commit -m test, git branch, git push, and git pull all printed the exact same message, and all exited with 128.
git diff behaved differently. Instead of an error, it printed a warning line, followed by a long usage description for --no-index (the exit code was 129).
$ git diff
warning: Not a git repository. Use --no-index to compare two paths outside a working tree
usage: git diff --no-index [<options>] <path> <path>
Diff output format options
(以下、オプションの一覧が続くので省略)
As described in the official git diff documentation, running git diff outside a repository is treated as a way to compare two files (or folders). In my test I ran it without giving any files, so it displayed the usage text.
How to read the message
(or any of the parent directories): .git means that neither the current folder nor any folder above it contains a .git. According to the official Git documentation for the GIT_DISCOVERY_ACROSS_FILESYSTEM environment variable, if there is no .git in the current folder, Git searches the parent folders.
This means that if you are inside a repository, you will not get this error even when you are in a subfolder. If you are outside the repository, such as in the folder one level above the clone, you get this error.
On the other hand, commands that do not use a repository, such as git --version and git config --global user.name, worked without errors in the same folder.
Fix 1: Move into the repository folder
For example, if you do not cd after git clone, you will get this error. git clone creates a new folder and places the repository inside it (git clone documentation). Your terminal stays in the same place as before the clone.
In my test, I cloned into a folder named my-app and then ran git status in the same folder. The clone succeeded, but the command still failed.
$ git clone /path/to/origin my-app
Cloning into 'my-app'...
done.
$ ls -A
my-app
$ git status
fatal: not a git repository (or any of the parent directories): .git
The clone source was a local repository I created for testing. ls -A shows that the repository is one level further down. If you do not know where the repository is, you can search for .git with find (here I limited the search to two levels down).
$ find . -maxdepth 2 -name .git
./my-app/.git
The folder that contains .git is the top level of the repository. After I ran cd my-app and then ran the command, the error was gone (exit code 0).
$ cd my-app
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$ git log --oneline
be80cab first commit
Running git status from a subfolder inside the repository, such as src, gave the same result. To find the top level of the repository, use --show-toplevel with git rev-parse. To check whether you are inside a repository, use --is-inside-work-tree.
$ cd src
$ git rev-parse --show-toplevel
/path/to/demo/my-app
$ git rev-parse --is-inside-work-tree
true
Also, when I ran git rev-parse --is-inside-work-tree outside a repository, it did not print false. Instead, it printed the same fatal: not a git repository … error as in this article (exit code 128).
Specifying the folder without cd
If you want to run a command once without changing folders, use git -C <folder>. As described in the official Git documentation for the -C option, the command runs as if Git had been started in the specified folder. When I ran it from the parent folder, it did not produce an error.
$ git -C my-app status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Fix 2: Use git init to start a new repository
If you want to start managing a folder with Git that is not yet under version control, create a repository with git init. git init creates a .git folder in that folder.
In my test, I used a folder that only had index.html. Before git init, the command failed. After git init, git status worked.
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ git init
Initialized empty Git repository in /path/to/demo2/.git/
$ ls -A
.git
index.html
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)
In the test environment, I configured the initial branch name to be main. After that, git add and git commit finished without errors.
$ git add index.html
$ git commit -m "first commit"
[main (root-commit) d0b474c] first commit
1 file changed, 1 insertion(+)
create mode 100644 index.html
If you get this error in a cloned project, do not run git init. First, cd into the repository folder. If you run git init in the folder one level above the clone, you will create a separate repository with no history.
In my test, I ran git init in the folder where I had cloned my-app (one level above my-app). The error stopped, but git status showed No commits yet, and my-app/ was listed as an untracked file. git log failed with a different error (exit code 128).
$ git init
Initialized empty Git repository in /path/to/demo/.git/
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
my-app/
nothing added to commit but untracked files present (use "git add" to track)
$ git log
fatal: your current branch 'main' does not have any commits yet
The history inside my-app was still intact. After I ran cd my-app and then git log --oneline, the output showed be80cab first commit. If you created a repository by mistake, you can undo it by deleting the .git folder that was created in that folder. In my test, after I ran rm -rf .git, git status returned to the original fatal: not a git repository … error. Delete only the .git in the folder where you ran git init by mistake.
The output of git init shows the folder where the repository was created, as Initialized empty Git repository in …/.git/. Check there that it is the folder you intended.




