Why does Git insist that a file has been modified even after a `git checkout --`?

馋奶兔 提交于 2019-12-12 11:45:39

问题


I made a change to a couple of files locally, without committing them. git status shows:

>> git status
# On branch feature-ravendb
# Your branch is ahead of 'origin/feature-ravendb' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   source/Octopus.Tentacle/Integration/PowerShell/IPowerShell.cs
#       modified:   source/Octopus.Tentacle/Integration/PowerShell/PowerShellRunner.cs
#
no changes added to commit (use "git add" and/or "git commit -a")

I want to discard the changes to those files. I tried following the instructions:

>> git checkout -- .\source\Octopus.Tentacle\Integration\PowerShell\IPowerShell.cs

The command has no output. Now I run git status again:

>> git status
# On branch feature-ravendb
# Your branch is ahead of 'origin/feature-ravendb' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   source/Octopus.Tentacle/Integration/PowerShell/IPowerShell.cs
#       modified:   source/Octopus.Tentacle/Integration/PowerShell/PowerShellRunner.c
#
no changes added to commit (use "git add" and/or "git commit -a")

Hmm, it's pretty convinced that the file has changed. And git diff <file> seems to think that the whole file has changed, even though it hasn't:

diff --git a/source/Octopus.Tentacle/Integration/PowerShell/IPowerShell.cs b/source/Octo
--- a/source/Octopus.Tentacle/Integration/PowerShell/IPowerShell.cs
+++ b/source/Octopus.Tentacle/Integration/PowerShell/IPowerShell.cs
@@ -1,9 +1,9 @@
-<EF><BB><BF>using System;
-
-namespace Octopus.Tentacle.Integration.PowerShell
-{
-    public interface IPowerShell
-    {
-        PowerShellExecutionResult Execute(PowerShellArguments arguments);
-    }
+<EF><BB><BF>using System;
+
+namespace Octopus.Tentacle.Integration.PowerShell
+{
+    public interface IPowerShell
+    {
+        PowerShellExecutionResult Execute(PowerShellArguments arguments);
+    }
 }
\ No newline at end of file

How do I convince git that I really, really haven't changed the file and don't want to commit it?

Edit: The following commands also have no effect on these modifications:

  • git checkout -- .
  • git checkout -f
  • git reset --soft
  • git reset --hard

Edit 2: Reverting back to an older revision, stashing my changes, then clearing the stash eventually worked. It seems like my line endings are conflicting but I don't know why:

  • I have core.autocrlf set to true
  • I have a .gitattributes file with the line *.cs text

Shouldn't this be enough?


回答1:


I had a core.autocrlf is false, but recently changed to true and also apply gitattributes changes for .net. After that, in many repositories, when you try to pull, I began to receive reports on the changes which not exist, and are also unable to roll back. In the end, I went back to core.autocrlf is false



来源:https://stackoverflow.com/questions/10017885/why-does-git-insist-that-a-file-has-been-modified-even-after-a-git-checkout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!