How to "resolve fatal: Not a git repository"?
Asked Answered
S

8

67

I was trying to remove one sub-module from the project

Tried rm -rf .git/modules/submodulePath

After that I am having the issue

fatal: Not a git repository

Shushubert answered 18/1, 2017 at 11:57 Comment(1)
list your folder structure if possible?Blondellblondelle
B
66

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
    
Blondellblondelle answered 18/1, 2017 at 12:7 Comment(6)
I am not able to find any folder related to the sub-module i removed.Shushubert
@Shushubert updated the normal way to do the removal of submodule in git. Go through it and let me know if you have any problemBlondellblondelle
In my case i need to do a git reset after editing the .git/config fileDelrio
Both of those files contain only relative paths for me. git version 2.27.0.Secretariat
I actually only had to do the submodule deinit here, thanks for the info!Prank
For some reason only a few of the submodules in my project contained absolute paths, the rest of them contained relative paths. I changed the absolute path entries to relative paths and everything worked.Fitz
E
19

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
Erk answered 27/8, 2018 at 0:28 Comment(4)
Exactly what I needed, short and sweet.Atheism
This was a saver for me!Held
The Windows equivalent for these should be rd /s /q <dir> for cmd or rd -r <dir> in powershell. https://mcmap.net/q/56117/-quot-rm-rf-quot-equivalent-for-windowsGourd
Actually, Remove-Item <dir> -Recurse -Force may be better for powershell #25798772Gourd
M
12

I've just hit this problem.

In my case, this was due to checkout branches with different submodules.

  • One branch has some submodules, but
  • the other has 2 more submodules.

For some reason, they were impossible to initialize with:

git submodule update --recursive --init

I've needed to manually delete this files/diretories before update worked properly.

{submodule-path}/.git
.git/modules/{submodule}/config
Marthamarthe answered 1/4, 2020 at 22:16 Comment(2)
Deleting the <submodule-path>/.git file fixed this issue for meEdging
For me alone with rm -rf submodule-path. Thanks ~Himyaritic
A
10

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

Aerodontia answered 13/12, 2017 at 14:14 Comment(0)
C
2

You simply can update the .git file

nano {my submodule}/.git

with the right gitdir.

Because i think you change the path of your folder

Chincapin answered 15/5, 2017 at 15:53 Comment(0)
A
1

In my case, When I try to git commit -m "***",this problrm occurs: fatal: not a git repository: library-sp18/../.git/modules/library-sp18

My solution: delete the .git file in library-sp18 and git init

Amperage answered 27/6, 2022 at 2:3 Comment(0)
E
1

I also experienced this issue when changing from my develop branch to a feature branch that contains a new submodule.

After testing all the above solutions, I noticed that the cause was a git config option: submodule.recurse = true

It was trying to fetch the module at checkout but the submodule was not well initialized.

Estriol answered 4/5, 2023 at 9:50 Comment(0)
T
0
  1. Remove the submodule section from the repository .git/config
  2. Delete submodule directores:
rm -rf {submodule-path}
rm -rf .git/modules/*

These are all the areas that submodule information is kept

Turbojet answered 18/11, 2022 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.