Possible Cause #1 - Line Ending Normalization
One situation in which this can happen is when the file in question was checked into the repository without the correct configuration for line endings (1) resulting in a file in the repository with either the incorrect line endings, or mixed line endings. To confirm, verify that git diff
shows only changes in line endings (these may not be visible by default, try git diff | cat -v
to see carriage returns as literal ^M
characters).
Subsequently, someone probably added a .gitattributes
or modified the core.autocrlf
setting to normalize line endings (2). Based on the .gitattributes
or global config, Git has applied local changes to your working copy that apply the line ending normalization requested. Unfortunately, for some reason git reset --hard
does not undo these line normalization changes.
Solution
Workarounds in which the local line endings are reset will not solve the problem. Every time the file is "seen" by git, it will try and reapply the normalization, resulting in the same issue.
The best option is to let git apply the normalization it wants to by normalizing all the line endings in the repo to match the .gitattributes
, and committing those changes -- see Trying to fix line-endings with git filter-branch, but having no luck.
If you really want to try and revert the changes to the file manually then the easiest solution seems to be to erase the modified files, and then to tell git to restore them, although I note this solution does not seem to work consistently 100% of the time (WARNING: DO NOT run this if your modified files have changes other than line endings!!):
git status --porcelain | grep "^ M" | cut -c4- | xargs rm
git checkout -- .
Note that, unless you do normalize the line endings in the repository at some point, you will keep running into this issue.
Possible Cause #2 - Case Insensitivity
The second possible cause is case insensitivity on Windows or Mac OS/X. For example, say a path like the following exists in the repository:
/foo/bar
Now someone on Linux commits files into /foo/Bar
(probably due to a build tool or something that created that directory) and pushes. On Linux, this is actually now two separate directories:
/foo/bar/fileA
/foo/Bar/fileA
Checking this repo out on Windows or Mac may result in modified fileA
that cannot be reset, because on each reset, git on Windows checks out /foo/bar/fileA
, and then because Windows is case insensitive, overwrites the content of fileA
with /foo/Bar/fileA
, resulting in them being "modified".
Another case may be an individual file(s) that exists in the repo, which when checked out on a case insensitive filesystem, would overlap. For example:
/foo/bar/fileA
/foo/bar/filea
There may be other similar situations that could cause such problems.
git on case insensitive filesystems should really detect this situation and show a useful warning message, but it currently does not (this may change in the future -- see this discussion and related proposed patches on the git.git mailing list).
Solution
The solution is to bring the case of files in the git index and the case on the Windows filesystem into alignment. This can either be done on Linux which will show the true state of things, OR on Windows with very useful open source utility Git-Unite. Git-Unite will apply the necessary case changes to the git index, which can then be committed to the repo.
Another useful tool with Git 2.16+ is the git add --renormalize <pathspec>
command. See https://stackoverflow.com/a/47580886/430128 and the git-add man page for more information:
--renormalize
core.autocrlf configuration or the text attribute in order to correct
files added with wrong CRLF/LF line endings. This option implies -u.
Lone CR characters are untouched, thus while a CRLF cleans to LF, a
CRCRLF sequence is only partially cleaned to CRLF.> Apply the "clean" process freshly to all tracked files to forcibly add
them again to the index. This is useful after changing
(1) This was most likely by someone using Windows, without any .gitattributes
definition for the file in question, and using the default global setting for core.autocrlf
which is false
(see (2)).
(2) http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/
.
stands for current directory not root directorygit
version? Either you are making a silly mistake or you have an old version that is buggy?