G S
G S

Reputation: 36838

Git: trailing whitespace causes file to remain in modified state

I have a git repo. There are some files committed into it which already contain trailing white space. Now if I ever make a change to any such file and then try to discard it with git checkout ., git undoes my changes alright, but the file still remains the modified state. No matter how many times I repeat git checkout . thereafter, the state does not change.

If I do a git diff at this point, it shows me that the whole file has changed, i.e. the full contents of the file have been replaced by new contents. Also doing a git diff --check (correctly) shows me the trailing whitespace errors that was originally present in the file.

The only way I can restore this file to unmodified state is by running git rm . --cached && git reset --hard, which is very awkward.

I understand that git is touchy about whitespace errors, but I do not expect the above behaviour from it. Can someone explain to me why this is happening? Is this a feature or a bug? Also, if it's normal behaviour, what's the best way to restore the file to unmodified state.

I'm running git version 1.8.3.msysgit.0 on Windows7 x64.

Upvotes: 1

Views: 289

Answers (1)

VonC
VonC

Reputation: 1326932

Make sure the all content doesn't change because of end of line conversion.

Do a:

git config --global core.autocrlf false

(It is set to true by default with msysgot)

Then try again your git checkout ..


The OP Frederick The Fool confirms in the comments:

It did eventually turn out to be due to line endings. I thought I had dealt with the line-ending problems using the method recommended here on GitHub.

But turns out the problem wasn't fixed in all files.
So what I had to do was to:

  1. arbitrarily modify all files in my repo.
  2. Do a git checkout . at the root of the repo.
  3. Since not all files would be undone by step 2, do a git commit -a on all of them.

These steps got rid of the problem permanently.

Upvotes: 2

Related Questions