How to "resolve fatal: Not a git repository"?

Solution 1:

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.

Update:

1.) Delete the relevant section from the .gitmodules file. You can use below command:

git config -f .gitmodules --remove-section "submodule.submodule_name"

2.) Stage the .gitmodules changes

git add .gitmodules

3.) Delete the relevant section from .git/config. You can use below command:

git submodule deinit -f "submodule_name"

4.) Remove the gitlink (no trailing slash):

git rm --cached path_to_submodule

5.) Cleanup the .git/modules:

rm -rf .git/modules/path_to_submodule

6.) Commit:

git commit -m "Removed submodule <name>"

7.) Delete the now untracked submodule files

rm -rf path_to_submodule

Solution 2:

I ran into this and didn't have a .git/modules directory in my main repository. I have one submodule 'build', so just removed any references and reinitialized it:

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

Solution 3:

I ran into this error after I moved a git repository to a different folder. When I looked in:

{submodule}/.git

I saw a single line with an absolute path, e.g.:

gitdir: /Users/ajx/Documents/repo/.git/modules/{submodule}

I changed this to a relative path, e.g.:

gitdir: ../../.git/modules/{submodule}

I'm not sure why git would hardcode absolute paths...