Git files modified after checkout, reset --hard, etc. even though autocrlf is set to false
Asked Answered
T

2

18

Here is my system dialogue:

unrollme-dev-dan:views Dan$ git reset --hard HEAD
HEAD is now at 3f225e9 Fix scan titles
unrollme-dev-dan:views Dan$ git status
# On branch master
# 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:   app/signup/finish.html
#   modified:   app/signup/scan.html
#

I have autocrlf set to false:

unrollme-dev-dan:unroll-website Dan$ git config core.autocrlf
unrollme-dev-dan:unroll-website Dan$ 
unrollme-dev-dan:unroll-website Dan$ git config --global core.autocrlf
unrollme-dev-dan:unroll-website Dan$

And I don't have any .gitattributes files messing this up:

unrollme-dev-dan:unroll-website Dan$ find . -name .gitattributes
[ only results are in different directory tree ]

This is caused by a .gitattributes one level up as pointed out in answer below.

When I do an od -c on the files it shows \r\n. I don't know what they "should" be, presumably, they should end in \n and that is why the diff is showing. But what I don't understand is how these files could possibly be modified on checkout even with autocrlf false.

What can cause git modifying a file on checkout besides autocrlf?

Tenorrhaphy answered 28/3, 2013 at 14:17 Comment(4)
May be a duplicate of this question, not voting to close until one of these has a correct answer that works for both.Tenorrhaphy
What OS btw? Windows I assume?Blotchy
@Blotchy Mac/Unix. It seems my home Mac dropped extra CRs anyway, I probably have nano config'ed wrong.Tenorrhaphy
Also check core.safecrlf. In theory, since core.eol and core.autocrlf are off, this shouldn't have any effect, but I haven't really played with all possible combinations of the three variables...Seay
C
21

This problem can be caused by gitattributes' text option https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

This problem can be fixed by temporarily editing your .gitattributes file within your project folder.

change * text=auto to #* text=auto make the necessary changes to files line endings and push your commit. You can then enable it again after the change has been made, or alternatively select one of the other options that may better suit your project.

Cellini answered 28/3, 2013 at 16:52 Comment(2)
Of course, if you're working on the same git repo you can notice there in fact was a .gitattributes file, just not one I found in my search...Tenorrhaphy
Thanks! My case was slightly different: a .jar file suddenly showing as modified after I added .gitattributes. I added a line in there with: *.jar binaryLammond
A
4

I didn't have a gitattributes file as is mentioned in the accepted answer, it was a file permissions problem in my case. To see if this is your problem check the changed files' differences with git diff, e.g.:

git diff path/to/file.html

If the only change you see is old mode/new mode, it's likely a permissions problem. You can tell git to ignore file permission changes using:

git config core.filemode false

or

git config --global core.filemode false

(depending on how you use git).

I recently switched from using Cygwin git to git for Windows for performance reasons, plus so that TortoiseGit works properly, this may be the reason permissions were thrown off in my case.

References:

  1. How do I remove files saying "old mode 100755 new mode 100644" from unstaged changes in Git?
Alsworth answered 14/5, 2015 at 15:51 Comment(1)
Thanks for core.filemode, that solved my problems on Windows.Loar

© 2022 - 2024 — McMap. All rights reserved.