How to Fix ‘fatal: not a git repository’ Error in Linux

Running a standard Git command—such as git status, git branch, or git log—in a Linux terminal only to be met with the error fatal: not a git repository (or any of the parent directories): .git is an everyday hurdle for developers, system administrators, and DevOps engineers. This error indicates that the Git binary traversed your current working directory and every parent directory up to the root filesystem (/) without discovering an initialised .git repository metadata folder. Whether you recently cloned a project, changed shell sessions, switched directories unexpectedly, or encountered file permission hurdles, resolving this fatal message requires navigating to the correct project root or initialising version control properly.

Why Does Git Return ‘fatal: not a git repository’?

Whenever you execute a Git command, the tool inspects your current directory for a hidden subfolder named .git. If it does not find one, it walks up the directory hierarchy one level at a time until it encounters the repository boundary (or reaches the root filesystem). The fatal error triggers due to several common scenarios:

  • Incorrect Current Working Directory: Opening a fresh terminal terminal session or script where the active path is ~ (user home) rather than the project source code directory.
  • Uninitialised Project Directory: Attempting to track a folder where git init has never been executed, or where a project was downloaded as a flat .zip or .tar.gz archive rather than cloned via git clone.
  • Accidental Deletion of the .git Directory: Cleaning up hidden files or running cleanup scripts that deleted the local version control repository database.
  • File Ownership or Safe Directory Restrictions: Modern Git security mechanisms (CVE-2022-24765) refusing to execute in repositories owned by a different Linux user account (such as root) without explicit configuration.

Step 1: Verify Current Directory and Hidden Files

The most frequent cause of this error is simply executing Git commands from outside the project repository boundary.

  1. Print your current working directory by typing:
    pwd
  2. List all files, including hidden dotfiles, to check if the .git directory exists:
    ls -la
  3. If you do not see a directory named .git in the output, navigate into your actual project folder:
    cd /path/to/your/actual/project
  4. Run git status again. If the working tree clean message appears, you are back inside your repository.

Step 2: Initialise a New Git Repository

If you created a new coding project from scratch or extracted a downloaded archive, Git must be initialised before it can execute tracking commands.

  1. Ensure you are in the root directory of your project folder.
  2. Execute the repository initialisation command:
    git init
  3. Git will output confirmation that an empty repository has been created (e.g., Initialized empty Git repository in /project/.git/).
  4. Stage your files and make your first commit:
    git add .
    git commit -m "Initial commit"
  5. Test your status again with git status to confirm the error is cleared.

Step 3: Fix ‘fatal: detected dubious ownership in repository’

On modern Linux distributions running Git 2.35.2 or later, running Git as a normal user in a directory owned by another user (or running inside Docker or shared network mounts) will trigger a dubious ownership error or cause Git to ignore the .git directory entirely.

  1. Check the ownership of your repository folder:
    ls -ld .git
  2. If the folder is owned by root while you are logged in as a standard user, change ownership back to your user account:
    sudo chown -R $USER:$USER .
  3. Alternatively, if you intentionally share the repository across multiple users or containers, add the directory to Git’s global safe directory configuration:
    git config --global --add safe.directory $(pwd)
  4. Or mark all directories as safe (useful in controlled development environments):
    git config --global --add safe.directory '*'

Step 4: Clone the Project Anew If .git Is Corrupted or Missing

If the project was originally cloned from a remote server (such as GitHub, GitLab, or Bitbucket) and the local .git metadata directory was accidentally removed, re-linking or re-cloning is the safest path forward.

  1. If you still have uncommitted local changes, create a backup copy of your directory:
    cp -r my-project my-project-backup
  2. Clone a fresh copy of the repository into a new folder:
    git clone https://github.com/username/repository.git my-project-clean
  3. Copy your modified source code files from the backup into the clean clone directory (taking care not to overwrite the fresh .git directory).
  4. Navigate into the clean directory and run:
    cd my-project-clean
    git status
  5. Git will now accurately highlight your modified files without throwing repository errors.

Get the best tech tips delivered straight to your inbox.

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