简体   繁体   中英

Git commits wrong newlines when using .gitattributes

I was having an issue where merge conflicts resulted in the entire file being a conflict. This ended up being that the local file's newlines all became unix-style newlines (LF) at the time of merge (before merge both development and feature branch had CRLF newlines when checked out).

If I ran

git rm --cached -r .
git add -A

no changes would show up with git status. But when I deleted the .gitattributes file and did another remove all / add all it resulted in certain files being updated with different new lines. For example for a 100 line file it would say 100 lines deleted, 100 lines added. After doing this to both branches the merges were fine.

.gitconfig autocrlf = true was set and .gitattributes file only had these lines. I would think the below lines would only affect merging strategry.

*.csproj -text merge=union
*.sln -text merge=union

Why would this .gitattributes change how new lines are committed?

Also with autocrlf set to true I'm not sure why the merge would be comparing LF's vs CRLF's unless maybe something in the .gitattributes was overriding it.


From https://help.github.com/articles/dealing-with-line-endings

Optionally, you can configure the way Git manages line endings on a per-repository basis by configuring a special .gitattributes file. This file is committed into the repository and overrides an individual's core.autocrlf setting, ensuring consistent behavior for all users, regardless of their Git settings. The advantage of a .gitattributes file is that your line configurations are associated with your repository

This makes it clear that .gitattributes is capable of overriding autocrlf, but there are no settings in it that tell it to do any eol conversion. Maybe there is some default that is implicitly used.

I realise this is an old question, but the answer might be useful for someone having a related issue.

The gitattributes file specifies "-text" for *.csproj and *.sln files. This means that for those files no EOL conversion should be done.

On the other hand, you also used the configuration "autocrlf = true". This setting means: store the files with LF line endings in the Git repository and store the files with native line endings in the working directory.

So: before adding the gitattributes file, the *.csproj and *.sln files are stored with LF line endings in the Git repo, because of the autocrlf setting. After adding the gitattributes file and checking out those files in the working directory, no EOL conversion is done, and those files end up with LF line endings in the working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM