git status returns fatal: Not a git repository but .git exists and HEAD has proper permissions

Git

Git Problem Overview


When I run git status on my repo I get fatal: Not a git repository: /my repo/.git/modules/docs

I've checked and .git exists and contains HEAD with the proper permissions. I can run various other commands fine. If I run git gui it opens fine and will list a couple of the changed files, but is missing a lot of them.

I'm guessing there may be some sort of corruption in HEAD, not sure though. Any idea how to fix this without wiping out the whole repo?

Update: I realized that I had changed the name of the repo's directory. The directory being referenced in the error is the old name of the directory. So my current repo is at /new dir/.git but the error is saying Not a git repository: /old dir/.git/modules/docs. So maybe git is confused?

Git Solutions


Solution 1 - Git

These two files contains absolute submodule path:

{submodule}/.git
.git/modules/{submodule}/config

So, if you moved the repo, the absolute path in these two files are not valid, and cause the 'not a git repository' error. Just fix these files manually.

Solution 2 - Git

Former versions of git used an absolute path to locate the gitdir of a submodule. The solution is as follows:

  1. Upgrade git to the latest version. Some says you'll need at least version 1.7.10. I just successfully solved the issue with git 1.8.3.
  2. Delete all the broken submodule folders: rm -rf broken_submodule_folder
  3. Update the registered submodules: git submodule update. You should see the submodules being checked out.

Solution 3 - Git

I finally sorted out that the issue was due to an issue with one of the submodules. Simply renaming the repo directory caused a conflict with that submodule. After seeing the discussion in https://stackoverflow.com/questions/9878860/how-can-i-rename-a-git-repository-with-submodules I realized that cloning the repo is a better way to go instead of renaming the directory and that solved the issue with the submodule.

Solution 4 - Git

In my case the problem was the .git/HEAD file didn't point anywhere, it just contained a sequence of strange characters. I copied the contents of .git/ORIG_HEAD to .git/HEAD and it worked again.

Source

Solution 5 - Git

I solved this issue by reseting all git-submodules with

rm -rf .git/modules
git submodule update --init

Solution 6 - Git

Following @ax003d answer, you could replace all old paths (old/path) with the new path (new/path) using this command:

find . -type f \( -name ".git" -o \( -path "*.git/modules/*" -name config \) \)  -print0 | xargs -0 sed -i -e "s#old/path#new/path#g"

You might want to check what the old paths look like before replacing them:

find . -type f \( -name ".git" -o \( -path "*.git/modules/*" -name config \) \)  -print0 | xargs -0 grep --colour "old/path"

Solution 7 - Git

Solution :

  1. Look into the HEAD file (under .git) -> If it is corrupted (contains some random values), replace the content with this text 'ref: refs/heads/develop' (develop is the last branch I was working on)

  2. try - git status.

  3. If step 2 doesn't solve your issue, try these commands (may not work for all but worth a try)

rm -f .git/index git reset

  1. git will give you unstaged files after reset. keep or discard as per your wish

Solution 8 - Git

I was facing this issue with submodules as well, but after analyzing the two files

{submodule}/.git .git/modules/submodules/{submodule}/config

I realized that in my case this was not an issue.

After a little research I have found that in my case I had to add the git (command : module add git) and the error disappeared.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosh FarnemanView Question on Stackoverflow
Solution 1 - Gitax003dView Answer on Stackoverflow
Solution 2 - GitMaxime R.View Answer on Stackoverflow
Solution 3 - GitJosh FarnemanView Answer on Stackoverflow
Solution 4 - GitEmir KuljaninView Answer on Stackoverflow
Solution 5 - GitQuanlongView Answer on Stackoverflow
Solution 6 - GitluissquallView Answer on Stackoverflow
Solution 7 - GitGhostView Answer on Stackoverflow
Solution 8 - GitTejasView Answer on Stackoverflow