如何“解决致命问题:不是 git 存储库”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41718822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:44:48  来源:igfitidea点击:

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

gitgit-submodules

提问by coreDeviOS

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

我试图从项目中删除一个子模块

Tried rm -rf .git/modules/submodulePath

试过 rm -rf .git/modules/submodulePath

After that I am having the issue

之后我遇到了这个问题

fatal: Not a git repository

致命:不是 git 存储库

回答by Shivkumar kondi

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.

因此,如果您移动了 repo,这两个文件中的绝对路径无效,并导致“not a git repository”错误。只需手动修复这些文件。

Update:

更新:

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

1.) 从 .gitmodules 文件中删除相关部分。您可以使用以下命令:

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

2.) Stage the .gitmodules changes

2.) 暂存 .gitmodules 更改

git add .gitmodules

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

3.) 从 .git/config 中删除相关部分。您可以使用以下命令:

git submodule deinit -f "submodule_name"

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

4.) 删除 gitlink(没有尾部斜杠):

git rm --cached path_to_submodule

5.) Cleanup the .git/modules:

5.) 清理 .git/modules:

rm -rf .git/modules/path_to_submodule

6.) Commit:

6.) 承诺:

git commit -m "Removed submodule <name>"

7.) Delete the now untracked submodule files

7.) 删除现在未跟踪的子模块文件

rm -rf path_to_submodule

回答by Jason Goemaat

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

我遇到了这个并且.git/modules在我的主存储库中没有目录。我有一个子模块“build”,所以只需删除所有引用并重新初始化它:

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

回答by Alec Jacobson

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

将 git 存储库移动到其他文件夹后,我遇到了此错误。当我查看时:

{submodule}/.git

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

我看到一行带有绝对路径,例如:

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

I changed this to a relativepath, e.g.:

我将其更改为相对路径,例如:

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

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

我不知道为什么 git 会硬编码绝对路径......

回答by Fernando De Brito Cerqueira

You simply can update the .git file

您只需更新 .git 文件

nano {my submodule}/.git

nano {我的子模块}/.git

with the right gitdir.

使用正确的 gitdir。

Because i think you change the path of your folder

因为我认为您更改了文件夹的路径

回答by kikeenrique

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.
  • 一个分支有一些子模块,但是
  • 另一个还有 2 个子模块。

For some reason, they were impossible to initialize with:

出于某种原因,它们无法初始化:

git submodule update --recursive --init

git submodule update --recursive --init

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

在正确更新措辞之前,我需要手动删除这些文件/目录。

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