Why is 'Updating the Git index failed' displayed

前端 未结 3 825
谎友^
谎友^ 2021-01-30 10:30

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

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

3条回答
  •  广开言路
    2021-01-30 11:14

    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.

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

    The warning you get is reproducible if you do the following on 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.autocrlf and git config core.safecrlf to verify that autocrlf is set to true and safecrlf is 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.txt in UNIX format. Write a file which has at least one line break. This is how you select UNIX line endings:

    • 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.txt vanishes from the working directory:

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

      git checkout master
      

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

    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=true together with core.safecrlf= 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.

提交回复
热议问题