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

8

65

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?

Hissing answered 13/4, 2012 at 15:51 Comment(2)
Are you nesting git repositories?Bendix
@Bendix I do have several submodules, however this issue is with the main repo.Hissing
H
9

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 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.

Hissing answered 16/4, 2012 at 20:2 Comment(3)
Recovering from this problem only takes two small manual edits per submodule. For me that's easier than re-cloning the repository and all its submodule(s).However
@JoshFarneman How should I (newbie) understand what you've done. pls provide clear code before and after. so one can understand what is required.Soni
But what If I don't want to lose my local branches. ? Please help me what to do in such a scenario.Beore
C
87

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.

Cachepot answered 15/12, 2012 at 15:40 Comment(3)
Thank you, this worked well for me. However, the second path was a bit shorter for me: .git/modules/{submodule}/config (I'm using git version 1.7.9.5 on Ubuntu)However
I just moved from PC to Mac and having the same problem. I updated the path in those two files, and the new path is correct, but I'm still getting fatal: Not a git repository. There is a .git file in that location, so maybe I'm missing something else?Pamphleteer
Just a note here, for me the config file has a relative path unlike the .git file.Balthazar
M
43

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.
Muley answered 30/5, 2013 at 15:12 Comment(1)
I know this is an old answer but just to let other people know: it's still a good answer in 2018. Thank you!Hecate
H
9

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 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.

Hissing answered 16/4, 2012 at 20:2 Comment(3)
Recovering from this problem only takes two small manual edits per submodule. For me that's easier than re-cloning the repository and all its submodule(s).However
@JoshFarneman How should I (newbie) understand what you've done. pls provide clear code before and after. so one can understand what is required.Soni
But what If I don't want to lose my local branches. ? Please help me what to do in such a scenario.Beore
R
9

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

Retrospect answered 14/3, 2017 at 5:57 Comment(0)
L
6

I solved this issue by reseting all git-submodules with

rm -rf .git/modules
git submodule update --init
Leigha answered 24/7, 2015 at 7:14 Comment(1)
Wow! Just worked for me! In my case I accidentally made a "rm -r" with no wildcard matching. It wiped a couple of files from the project root before being stopped for some prompt.Bartholemy
C
4

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"
Crock answered 5/9, 2014 at 0:17 Comment(0)
C
3

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

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

Caducous answered 30/11, 2018 at 11:7 Comment(2)
Thank you, this fixed it for me. I'm guessing my HEAD file somehow got corrupted. No submodules like OP, just simply receiving the dreadful "Fatal: not a git repository" all of a sudden out of nowhereRomanaromanas
This happened to me when i was doing git checkout then it hanged so i restarted the pc. thank you mate.Goodbye
I
1

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.

Ingrown answered 16/6, 2014 at 8:39 Comment(1)
In Linux command prompt while running - module add got, I get the following error. module: command not foundChar

© 2022 - 2024 — McMap. All rights reserved.