64

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?

Josh Farneman
  • 1,729
  • 1
  • 14
  • 19

8 Answers8

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.

void.pointer
  • 24,859
  • 31
  • 132
  • 243
ax003d
  • 3,278
  • 29
  • 26
  • 10
    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) – Drew Noakes Jan 29 '13 at 17:04
  • 1
    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? – Jo Sprague Jan 15 '14 at 17:08
  • 4
    Just a note here, for me the config file has a relative path unlike the .git file. – Loïc Faure-Lacroix Mar 13 '17 at 15:50
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.
Maxime R.
  • 9,621
  • 7
  • 53
  • 59
  • I know this is an old answer but just to let other people know: it's still a good answer in 2018. Thank you! – ApusApus Nov 19 '18 at 22:55
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.

Community
  • 1
  • 1
Josh Farneman
  • 1,729
  • 1
  • 14
  • 19
  • 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). – Drew Noakes Mar 06 '13 at 23:24
  • 2
    @JoshFarneman How should I (newbie) understand what you've done. pls provide `clear code` before and after. so one can understand what is required. – alex Apr 06 '17 at 17:32
  • But what If I don't want to lose my local branches. ? Please help me what to do in such a scenario. – Dinesh Patil Dec 02 '21 at 06:03
8

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

Community
  • 1
  • 1
Emir Kuljanin
  • 3,881
  • 1
  • 25
  • 30
6

I solved this issue by reseting all git-submodules with

rm -rf .git/modules
git submodule update --init
Quanlong
  • 24,028
  • 16
  • 69
  • 79
  • 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. – Sankalp Jun 08 '16 at 09:27
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"
Community
  • 1
  • 1
luissquall
  • 1,740
  • 19
  • 14
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

Ghost
  • 111
  • 1
  • 3
  • 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 nowhere – Wirsing Oct 05 '21 at 22:46
  • This happened to me when i was doing git checkout then it hanged so i restarted the pc. thank you mate. – vexingCoder Jul 06 '22 at 10:19
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.

Community
  • 1
  • 1
Tejas
  • 11
  • 1
  • In Linux command prompt while running - module add got, I get the following error. module: command not found – rishi jain Feb 29 '20 at 16:33