This error occurs when git rev-parse or other Git commands are executed outside of an initialized Git repository. Git cannot locate the .git directory in the current path or any parent directories.
The "fatal: not a git repository" error from git rev-parse indicates that Git cannot find a valid repository in your current working directory or any of its parent directories. Git uses the .git directory to store all repository metadata, configuration, and history. When you run git rev-parse or any other Git command, the first thing Git does is search upward through the directory tree looking for this .git folder. The git rev-parse command is a plumbing command that's used internally by Git and by many third-party tools to parse revision parameters, find the repository root, and perform various repository-related queries. It's commonly invoked with flags like --git-dir, --show-toplevel, or --is-inside-work-tree to detect and locate Git repositories. When it cannot find the .git directory, it fails with this fatal error and exits with a non-zero status code. This error is particularly common in build systems, CI/CD pipelines, IDE integrations, and automated scripts that assume they're running within a Git repository context but are actually executing in a non-repository directory or an improperly initialized workspace.
First, confirm where you are in the filesystem and check if a .git directory exists:
pwd
ls -la | grep .gitIf you don't see a .git directory, you're not in a Git repository.
If you intended to run commands in a specific repository, navigate there:
cd /path/to/your/repository
git rev-parse --is-inside-work-treeThe last command should return true if you're now inside a valid Git repository.
If you're starting a new project and need to create a repository:
git initThis creates a new .git directory and initializes an empty Git repository. Alternatively, if you're setting up an existing remote repository:
git clone https://github.com/username/repository.git
cd repositoryIf you believe you're in a repository but still seeing the error, verify repository integrity:
# Check if .git is a directory (not a file or symlink)
file .git
# Verify HEAD file exists and is valid
cat .git/HEAD
# Run Git's integrity check
git fsck --fullIf HEAD is missing or corrupted, you may need to restore it or re-clone the repository.
If the error occurs in automated scripts or build systems, ensure:
# In CI/CD: Verify checkout step completed
git status
# In scripts: Use absolute paths or verify working directory
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$REPO_ROOT" ]; then
echo "Error: Not in a Git repository"
exit 1
fi
# In Docker: Ensure repository is mounted or cloned
docker run -v $(pwd):/workspace myimageFor build systems like ESP-IDF that check Git for version detection, ensure the repository is properly initialized before the build process starts.
Git Repository Detection Mechanism: Git searches for a .git directory starting from the current working directory and walking up the directory tree until it reaches the filesystem root or a mount point. This behavior can be controlled with the GIT_DIR environment variable or the --git-dir command-line option, which can override automatic detection.
CDPATH Conflicts: If you've exported the CDPATH environment variable in your shell, it can cause unexpected directory changes. If a subdirectory name in your PATH matches one in CDPATH, cd commands may navigate to the wrong location, taking you outside the repository. Consider unsetting CDPATH in Git-related scripts.
IDE and Tool Integration: Many development tools (VSCode, IntelliJ, etc.) and Git-dependent packages (git-lfs, GitLab Runner, etc.) use git rev-parse internally to detect repository boundaries and metadata. These tools may repeatedly call git rev-parse in background processes, causing log spam if you're working in non-repository directories. You can usually disable Git integration for specific workspaces in tool settings.
Submodules and Worktrees: In Git submodules and worktrees, the .git entry is sometimes a file (not a directory) containing a gitdir reference pointing to the actual Git directory. If this file is corrupted or points to a non-existent location, you'll see this error even though the structure looks correct.
Performance Considerations: Some tools check for Git repositories too eagerly. If performance is impacted by repeated failed git rev-parse calls, consider disabling automatic Git detection in tools or use .gitignore patterns to exclude large non-repository directories from scanning.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server