How to Fix ‘fatal: not a git repository (or any of the parent directories): .git’ in Linux

Executing git commands such as git status, git branch, or git log in a Linux terminal only to be stopped by the error message: ‘fatal: not a git repository (or any of the parent directories): .git’ halts source code management. In Git architecture, every version-controlled project is anchored by a hidden .git directory situated at the project’s root level, containing object databases, reference pointers, and configuration parameters. When you run a git command, Git searches the current directory and ascends parent directories recursively looking for this marker. If no .git folder is found, or if file permissions prevent reading it, Git aborts immediately.

Why Does Git Report ‘Not a Git Repository’?

The Git CLI requires a valid repository boundary before executing version control operations. Common causes for this error include:

  • Incorrect Working Directory: Opening a fresh terminal session and executing git commands before using cd to navigate into the cloned project directory.
  • Uninitialised Directory: Creating a new project folder without executing git init to construct the local Git database structure.
  • Accidentally Deleted or Corrupted .git Directory: Running aggressive cleanup scripts (e.g. rm -rf .*) that erased the hidden .git metadata folder.
  • Safe Directory Ownership Mismatch: Git security protections (introduced in Git 2.35.2+) blocking access to repositories owned by a different user account on the system.
  • Operating in Detached Subdirectories or Build Folders: Running git from temporary build directories that sit outside the repository working tree.

Step 1: Verify Current Directory and Navigate to Project Root

Confirm that your shell session is currently inside the actual cloned project folder.

  1. Check your current working directory path:
    pwd
  2. Inspect directory contents, including hidden files, to confirm if .git is present:
    ls -la
  3. If the .git folder is missing, navigate into your actual project workspace:
    cd /path/to/your/actual/project
  4. Re-run:
    git status

Step 2: Initialise a New Repository with git init

If you are starting a new project or downloaded source code as a plain ZIP archive without version history, initialise a new repository.

  1. Navigate to your project root folder:
    cd /path/to/project
  2. Initialise the Git repository structure:
    git init
  3. Set the default branch name to main:
    git branch -M main
  4. Stage and commit your existing project files:
    git add .
    git commit -m "Initial commit"
  5. Link your remote repository (GitHub, GitLab, Bitbucket):
    git remote add origin https://github.com/username/repository.git

Step 3: Fix ‘Safe Directory’ Ownership Warnings

If the repository is located on a shared volume or created under a different user (e.g. root or www-data), Git blocks access by default.

  1. If Git reports a dubious ownership warning alongside the error, add the directory to your global safe directory list:
    git config --global --add safe.directory /path/to/project
  2. Alternatively, change ownership of the directory tree to your active Linux user:
    sudo chown -R $USER:$USER /path/to/project

Step 4: Restore a Missing .git Folder from Remote Clone

If the .git folder was accidentally deleted but the remote repository remains available on GitHub/GitLab:

  1. Clone a fresh copy of the repository into a separate temporary directory:
    git clone https://github.com/username/repository.git /tmp/repo_temp
  2. Copy the intact .git metadata folder into your existing working project:
    cp -r /tmp/repo_temp/.git /path/to/your/project/
  3. Remove the temporary clone:
    rm -rf /tmp/repo_temp
  4. Return to your project folder and run git status to verify complete recovery of branch tracking and history.

Step 5: Verify GIT_DIR Environment Variables

An incorrectly set GIT_DIR environment variable forces Git to look in the wrong directory.

  1. Check if GIT_DIR is defined in your shell:
    echo $GIT_DIR
  2. If set to an invalid path, unset it:
    unset GIT_DIR

Navigating to the correct folder, running git init on new projects, and configuring safe directory permissions resolves ‘fatal: not a git repository’ in Linux.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.