windows 为什么显示“更新 Git 索引失败”

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

Why is 'Updating the Git index failed' displayed

windowsgitnewlinecore.autocrlf

提问by byronyasgur

I am using Windows. When staging files I get this error.

我正在使用 Windows。当暂存文件时,我收到此错误。

Updating the Git index failed. A rescan will be automatically started to resynchronize git-gui.

Updating the Git index failed. A rescan will be automatically started to resynchronize git-gui.

followed by a list of files which have been converted from LF to CRLF

后跟已从 LF 转换为 CRLF 的文件列表

After a lot of reading up on the CRLF / LF issue with Git usage cross platform, I more or less understand what is going on, and I am trying to determine which autocrlf setting is best for me, but I cant understand why Git says that Updating the index failed. My understanding is that it has converted the EOF's so what is the problem with that and why is it telling me that updating the index has failed. Do I need to fix something ( other than picking an appropriate autocrlf setting) or can I just proceed

在阅读了大量关于 Git 使用跨平台的 CRLF / LF 问题后,我或多或少了解了正在发生的事情,并且我正在尝试确定哪种 autocrlf 设置最适合我,但我不明白为什么 Git 这么说更新索引失败。我的理解是它已经转换了 EOF,所以有什么问题,为什么它告诉我更新索引失败。我需要修复一些东西吗(除了选择合适的 autocrlf 设置)还是我可以继续

I then have two options Continue and Unlock Index, what do these mean and what is the best course of action.

然后我有两个选项继续和解锁索引,这些是什么意思,什么是最好的行动方案。

回答by VonC

git config --global core.autocrlf false

Has always been my recommendation (see "Git 1.6.4 beta on Windows (msysgit) - Unix or DOS line termination").

一直是我的推荐(请参阅“ Windows 上的 Git 1.6.4 beta (msysgit) - Unix 或 DOS 行终止”)。

However, in your case, you can "Continue", but this warning is there to mention the conversion of certain files might not be reversible:

但是,在您的情况下,您可以“继续”,但此警告是为了提及某些文件的转换可能不可逆:

core.safecrlf

If true, makes git check if converting CRLF is reversible when end-of-line conversion is active. Git will verify if a command modifies a file in the work tree either directly or indirectly. For example, committing a file followed by checking out the same file should yield the original file in the work tree. If this is not the case for the current setting of core.autocrlf, git will reject the file.
The variable can be set to "warn", in which case git will only warn about an irreversible conversion but continue the operation.

如果为 true,当行尾转换处于活动状态时,让 git 检查转换 CRLF 是否可逆。Git 将验证命令是否直接或间接修改了工作树中的文件。例如,提交一个文件然后检出同一个文件应该会在工作树中产生原始文件。如果当前设置不是这种情况core.autocrlf,git 将拒绝该文件。
该变量可以设置为“warn”,在这种情况下,git 只会对不可逆转的转换发出警告,但会继续操作。

If you don't want to see this warning, as explained in this thread, you can set core.safecrlfto false.

如果您不想看到此警告,如该线程中所述,您可以设置core.safecrlffalse

You could also stash your files through the tools menu of git gui, and add some options to those tools with, for instance, this git config file.
The interest is that, for each tool, you can add:

您还可以通过 git gui 的工具菜单隐藏您的文件,并使用例如这个git config file为这些工具添加一些选项。
有趣的是,对于每个工具,您可以添加:

guitool.<name>.norescan

Don't rescan the working directory for changes after the tool finishes execution.

工具完成执行后,不要重新扫描工作目录以进行更改。



Could you please elaborate a bit on Unlock Index

你能否详细说明一下解锁索引

you can see that message in the index.tclgit-gui script: it removes the index.lock file the git-gui creates when manipulating the index.
You can see more at the "lockfile API" documentation page:

您可以在index.tclgit-gui 脚本中看到该消息:它删除了 git-gui 在操作索引时创建的 index.lock 文件。
您可以在“lockfile API”文档页面看到更多信息

Mutual exclusion.
When we write out a new index file, first we create a new file $GIT_DIR/index.lock, write the new contents into it, and rename it to the final destination $GIT_DIR/index.
We try to create the $GIT_DIR/index.lockfile with O_EXCLso that we can notice and fail when somebody else is already trying to update the index file.

相互排斥
当我们写出一个新的索引文件时,首先我们创建一个新文件$GIT_DIR/index.lock,将新内容写入其中,并将其重命名为最终目的地$GIT_DIR/index
我们尝试创建$GIT_DIR/index.lock文件,O_EXCL以便我们可以在其他人已经尝试更新索引文件时注意到并失败。

回答by Kenny Evitt

I also ran into this even tho my core.autocrlfsetting is already falseand core.safecrlfis unset. I suspect the culprit is the config setting diff.astextplain.textconv.

即使我的core.autocrlf设置已经设置false并且core.safecrlf未设置,我也遇到了这个问题。我怀疑罪魁祸首是配置设置diff.astextplain.textconv

When I ran git config --list, the following line was shown in the output:

当我运行时git config --list,输出中显示以下行:

diff.astextplain.textconv=astextplain

I don't think this setting is actually related to the warning/error but it inspired me to look into text conversion that might be being done. After a little spelunking online and in my repo, I discovered the following line in my repo's .gitattributesfile:

我不认为这个设置实际上与警告/错误有关,但它激励我研究可能正在完成的文本转换。在网上和我的 repo 中进行了一些探索之后,我在我的 repo 的.gitattributes文件中发现了以下行:

* text=auto

[I probably got the .gitattributesfile from GitHub.]

[我可能从 GitHub得到了.gitattributes文件。]

Given that only the above line was un-commented in it, and further that dealing with 'automagic' line-ending conversions has alwaysbeen a headache, I opted to remove that file from my repo. After doing so, staging the same files no longer prompted me with the "Updating the Git index failed" warning/error.

鉴于只有上面的行没有被注释掉,而且处理“自动”行尾转换一直是一个令人头疼的问题,我选择从我的存储库中删除该文件。这样做之后,暂存相同的文件不再提示我“更新 Git 索引失败”警告/错误。

回答by akraf

TL;DR:This warning means that git might return you a text file in Windows-style despite you having checked in a text file in UNIX-style.

TL;DR:这个警告意味着 git 可能会返回一个 Windows 风格的文本文件,尽管你已经签入了一个 UNIX 风格的文本文件。

UNIX and Windows differ in how they save line breaks in text files. Wikipedia has a list of line breaks on different OSes

UNIX 和 Windows 的不同之处在于它们在文本文件中保存换行符的方式。维基百科有不同操作系统的换行符列表

The warning you get is reproducible if you do the following on Windows:

如果您在 Windows 上执行以下操作,您收到的警告是可重现的:

  • Create a git repository in an empty directory
  • Create a commit representing the initial, empty state of the repo:

    git commit --allow-empty -m "initial commit"
    
  • use git config core.autocrlfand git config core.safecrlfto verify that autocrlfis set to trueand safecrlfis unset (no output). If this is not the case, use the following commands to set them

    git config core.autocrlf true
    git config --unset core.safecrlf
    
  • Use Notepad++to write a text file called text.txtin UNIX format. Write a file which has at least one line break. This is how you select UNIX line endings: Notepad++ with the menu Edit - EOL Conversion opened

  • git add text.txt. You get the warning message

    warning: LF will be replaced by CRLF in text.txt.
    The file will have its original line endings in your working directory.

  • Commit the text file: `git commit -m "add file with UNIX endings"

  • Now see how the file looks like if you check it out from the tree. First, check out the version before you created the file (go 1 commit back). The file text.txtvanishes from the working directory:

    git checkout ~1
    
  • Now, restore the version after you created the file

    git checkout master
    
  • 在空目录下创建一个git仓库
  • 创建一个代表 repo 初始空状态的提交:

    git commit --allow-empty -m "initial commit"
    
  • 使用git config core.autocrlfandgit config core.safecrlf来验证autocrlf设置为truesafecrlf未设置(无输出)。如果不是这种情况,请使用以下命令进行设置

    git config core.autocrlf true
    git config --unset core.safecrlf
    
  • 使用Notepad++编写一个text.txt以 UNIX 格式调用的文本文件。写一个至少有一个换行符的文件。这是您选择 UNIX 行尾的方式: Notepad++ 与菜单编辑 - EOL 转换打开

  • git add text.txt. 您收到警告消息

    警告:LF 将被 text.txt 中的 CRLF 替换。
    该文件将在您的工作目录中以原始行结尾。

  • 提交文本文件:`git commit -m "添加带有 UNIX 结尾的文件"

  • 现在,如果您从树中检出该文件,请查看该文件的外观。首先,在创建文件之前检查版本(返回 1 次提交)。该文件text.txt从工作目录中消失:

    git checkout ~1
    
  • 现在,在创建文件后恢复版本

    git checkout master
    

The file text.txtis restored. But open it in Notepad++ and check the line ending format in the bottom status line of Notepad++:

文件text.txt已恢复。但是在 Notepad++ 中打开它并检查 Notepad++ 底部状态行中的行尾格式:

The restored file which has now Windows-style CRLF endings

恢复的文件现在具有 Windows 风格的 CRLF 结尾

The file you checked out has Windows-style line endings, yet the file you commited had UNIX-style file endings! This is what the warning message is about: The settings core.autocrlf=truetogether with core.safecrlf=<unset>mean that the files you get restored from the tree might be different from the files you checked in, because they might have different file endings.

您签出的文件具有 Windows 样式的行尾,而您提交的文件具有 UNIX 样式的文件尾!这就是警告消息的内容:这些设置core.autocrlf=true与 一起core.safecrlf=<unset>意味着您从树中恢复的文件可能与您签入的文件不同,因为它们可能具有不同的文件结尾。