使用 Git 时如何解决“错误:索引错误 – 致命:索引文件损坏”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1115854/
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
How to resolve "Error: bad index – Fatal: index file corrupt" when using Git
提问by Number8
After git init
, I added and committed a few files, made some changes, added and committed. Set up the git daemon (running under Cygwin on WinXP) and cloned the repository once.
Now, I get this error with the cloned repository:
之后git init
,我添加并提交了一些文件,进行了一些更改,添加并提交。设置 git 守护进程(在 WinXP 上的 Cygwin 下运行)并克隆一次存储库。现在,我在克隆的存储库中收到此错误:
$ git status
error: bad index file sha1 signature
fatal: index file corrupt
Is there any way to fix this, other than getting a new copy of the repository?
除了获取存储库的新副本之外,还有什么方法可以解决此问题?
回答by Jakub Nar?bski
If the problem is with the index as the staging area for commits(i.e. .git/index
), you can simply remove the index (make a backup copy if you want), and then restore index to version in the last commit:
如果问题是索引作为提交的暂存区(即.git/index
),您可以简单地删除索引(如果需要,请制作备份副本),然后将索引恢复到上次提交中的版本:
On OSX/Linux:
在 OSX/Linux 上:
rm -f .git/index
git reset
On Windows:
在 Windows 上:
del .git\index
git reset
(The reset
command above is the same as git reset --mixed HEAD
)
(reset
上面的命令与 相同 git reset --mixed HEAD
)
You can alternatively use lower level plumbinggit read-tree
instead of git reset
.
您也可以使用较低级别的管道git read-tree
代替git reset
.
If the problem is with index for packfile, you can recover it using git index-pack
.
如果问题出在packfile 的索引上,您可以使用git index-pack
.
回答by hobs
You may have accidentally corrupted the .git/index file with a sed on your project root (refactoring perhaps?) with something like:
您可能不小心使用项目根目录上的 sed 损坏了 .git/index 文件(可能是重构?),例如:
sed -ri -e "s/$SEACHPATTERN/$REPLACEMENTTEXT/g" $(grep -Elr "$SEARCHPATERN" "$PROJECTROOT")
to avoid this in the future, just ignore binary files with your grep/sed:
为避免将来发生这种情况,只需忽略带有 grep/sed 的二进制文件:
sed -ri -e "s/$SEACHPATTERN/$REPLACEMENTTEXT/g" $(grep -Elr --binary-files=without-match "$SEARCHPATERN" "$PROJECTROOT")
回答by Cleiton Almeida
I had that problem, and I try ti fix with this:
我遇到了这个问题,我尝试解决这个问题:
rm -f .git/index
git reset
BUT it did not work. The solution?
For some reason I had others .git folders in sub directories. I delete those .git folders (not the principal) and git reset
again. Once they were deleted, everything worked again.
但它没有用。解决办法?出于某种原因,我在子目录中有其他 .git 文件夹。我git reset
一次又一次地删除那些 .git 文件夹(不是主体)。一旦它们被删除,一切又恢复了。
回答by Gav
This sounds like a bad clone. You could try the following to get (possibly?) more information:
这听起来像一个糟糕的克隆。您可以尝试以下操作来获取(可能?)更多信息:
git fsck --full
回答by eskimwier
Since the above solutions left me with continued problems, I used this dull solution:
由于上述解决方案给我留下了持续的问题,我使用了这个乏味的解决方案:
- clone a new copy of the repo elsewhere
- copy the fresh .git directory into the (broken) repo that contained the changes I wanted to commit
- 在别处克隆 repo 的新副本
- 将新的 .git 目录复制到包含我想要提交的更改的(损坏的)存储库中
Did the trick. Btw, I did a sed
on the project root as @hobs guessed. Learned my lesson.
做到了。顺便说一句,sed
正如@hobs 猜测的那样,我在项目根目录上做了一个。吸取了我的教训。
回答by Eighty
This worked for me. Although i'm curious of the reason I started getting the errors in the first place. When I logged out yesterday, it was fine. Log in this morning, it wasn't.
这对我有用。虽然我很好奇我首先开始收到错误的原因。当我昨天注销时,一切正常。今天早上登录,它不是。
rm .git/index
git reset
回答by jenming
Note for git submodule users - the solutions here will not work for you as-is.
请注意 git 子模块用户 - 此处的解决方案不适用于您。
Let's say you have a parent repository called dev
, for example, and your submodule repository is called api
.
dev
例如,假设您有一个名为 的父存储库,并且您的子模块存储库名为api
。
if you are inside of api
and you get the error mentioned in this question:
如果你在里面api
并且你得到这个问题中提到的错误:
error: bad index file sha1 signature
fatal: index file corrupt
error: bad index file sha1 signature
fatal: index file corrupt
The index
file will NOT be inside of a .git
folder. In fact, the .git
won't even be a folder - it will will be a text document with the location of the real .git data for this repository. Likely something like this:
该index
文件不会在.git
文件夹内。事实上,.git
它甚至不会是一个文件夹——它将是一个文本文档,其中包含此存储库的真实 .git 数据的位置。可能是这样的:
~/dev/api $ cat .git
gitdir: ../.git/modules/api
~/dev/api $ cat .git
gitdir: ../.git/modules/api
So, instead of rm -f .git/index
, you will need to do this:
因此,rm -f .git/index
您需要执行以下操作,而不是:
rm -f ../.git/modules/api/index
git reset
rm -f ../.git/modules/api/index
git reset
or, more generally,
或者,更一般地说,
rm -f ../.git/modules/INSERT_YOUR_REPO_NAME_HERE/index
git reset
rm -f ../.git/modules/INSERT_YOUR_REPO_NAME_HERE/index
git reset
回答by Nick Kuijpers
This issue can occur when there is a .git
directory underneath one of the subdirectories. To fix it, check if there are other .git directories there, and remove them and try again.
当其中一个.git
子目录下有一个目录时,可能会发生此问题。要修复它,请检查那里是否还有其他 .git 目录,然后删除它们并重试。
回答by Astra Uvarova - Saturn's star
I did a simple trick. I clone the repo to a new folder. Copied the .git folder from the new folder to repo's old folder, replacing .git there.
我做了一个简单的技巧。我将 repo 克隆到一个新文件夹。将 .git 文件夹从新文件夹复制到 repo 的旧文件夹,替换那里的 .git。
回答by Christopher Shaw
rm -f .git/index
git reset
More info at https://www.chris-shaw.com/blog/quick-fix-for-git-corrupt-index
更多信息请访问https://www.chris-shaw.com/blog/quick-fix-for-git-corrupt-index