git status 返回致命:不是 git 存储库,但 .git 存在并且 HEAD 具有适当的权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10144149/
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-10 13:26:27  来源:igfitidea点击:

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

git

提问by Josh Farneman

When I run git status on my repo I get fatal: Not a git repository: /my repo/.git/modules/docs

当我在我的 repo 上运行 git status 时,我得到 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.

我已经检查过 .git 存在并且包含具有适当权限的 HEAD。我可以正常运行各种其他命令。如果我运行 git gui 它可以正常打开并列出几个更改过的文件,但缺少很多文件。

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?

我猜测 HEAD 中可能存在某种损坏,但不确定。知道如何在不清除整个回购的情况下解决这个问题吗?

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/.gitbut the error is saying Not a git repository: /old dir/.git/modules/docs. So maybe git is confused?

更新:我意识到我已经更改了 repo 目录的名称。错误中引用的目录是目录的旧名称。所以我目前的回购是在/new dir/.git但错误是说Not a git repository: /old dir/.git/modules/docs。所以也许git很困惑?

采纳答案by Josh Farneman

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.

我终于弄清楚问题是由于其中一个子模块的问题造成的。简单地重命名 repo 目录会导致与该子模块发生冲突。在看到如何使用子模块重命名 git 存储库中的讨论后我意识到克隆 repo 是一种更好的方法,而不是重命名目录,并且解决了子模块的问题。

回答by ax003d

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”错误。只需手动修复这些文件。

回答by Maxime R.

Former versions of git used an absolute path to locate the gitdir of a submodule. The solution is as follows:

以前的 git 版本使用绝对路径来定位子模块的 gitdir。解决方法如下:

  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.
  1. 将 git 升级到最新版本。有人说你至少需要 1.7.10 版本。我刚刚用 git 1.8.3 成功解决了这个问题。
  2. 删除所有损坏的子模块文件夹: rm -rf broken_submodule_folder
  3. 更新注册的子模块:git submodule update. 您应该会看到正在检出的子模块。

回答by Quanlong

I solved this issue by reseting all git-submodules with

我通过重置所有 git-submodules 解决了这个问题

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

回答by luissquall

Following @ax003d answer, you could replace all old paths (old/path) with the new path (new/path) using this command:

在@ax003d回答之后,您可以使用以下命令将所有旧路径 ( old/path) 替换为新路径 ( new/path):

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"

回答by Emir Kuljanin

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.

就我而言,问题是 .git/HEAD 文件没有指向任何地方,它只包含一系列奇怪的字符。我将 .git/ORIG_HEAD 的内容复制到 .git/HEAD 并再次运行。

Source

来源

回答by Tejas

I was facing this issue with submodules as well, but after analyzing the two files

我也遇到了子模块的这个问题,但是在分析了两个文件之后

{submodule}/.git .git/modules/submodules/{submodule}/config

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

经过一番研究,我发现在我的情况下,我必须添加 git(命令:module add git)并且错误消失了。

回答by Arpit

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)

1)查看 HEAD 文件(在 .git 下)-> 如果它已损坏(包含一些随机值),请将内容替换为“ref: refs/heads/develop”(develop 是我正在处理的最后一个分支) )

2) try - git status.

2) 尝试 - git 状态。

3) If step 2 doesn't solve your issue, try these commands (may not work for all but worth a try)

3) 如果第 2 步没有解决您的问题,请尝试这些命令(可能对所有人都不起作用,但值得一试)

rm -f .git/index git reset

rm -f .git/index git reset

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

4) git 将在重置后为您提供未暂存的文件。根据您的意愿保留或丢弃