Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to normalize line endings for Git in Visual Studio Project

Problem: Git is interpreting an entire C# file as modified after saving file changes locally. It is indicating that line endings have changed, even though they were checked out as CRLF endings and saved locally with CRLF line endings.

I am working on a Visual Studio project at my job that has multiple contributors. I am using Visual Studio 2012. For the past 2 days, another colleague and I have been experienced the above issue after cloning the repo from our corporate Stash site. We cannot commit our changes because Git indicates that the entire file has changed and some of our files are thousands of lines long. Here is more context around the symptoms:

  • The problem only occurs in some files, not all.
  • The problem is not specific to Visual Studio. I edited the file in IntelliJ and Notepad++ and experienced the same issue
  • After the file is modified, I have extreme difficulty discarding changes and checking out the file from the repo. I have tried performing several commands which should reset my local repo. Sometimes they work and I am aligned with the origin, sometimes they don't. The only consistent solution is to checkout a new branch, commit the changes there, and switch back to the target branch. I have tried the following methods to discard changes:

    git checkout -- 
    git reset --hard
    git reset HEAD
    git stash

Here is a list of the steps I have taken to attempt fixing the issue:

  • Line ending normalization, as documented here
  • Changing my git config to autocrlf=true and autocrlf=false. This is documented here. The issue remains under both settings
  • Changing the .gitattributes settings in the project. The following are my current settings, which conform to the example given in the link above

    * text=auto
    *.cs text
  • Uninstalling and reinstalling Git (twice)

Nothing I have done so far has fixed the issue. My hunch is that it is related to the settings in the .gitattributes file. However, no one else, including the main developer on the project, have experienced these issues when cloning the repo. I would greatly appreciate any suggestions. Thanks!

like image 331
Eric Honsey Avatar asked Aug 14 '14 16:08

Eric Honsey


People also ask

Do you want to normalize line endings?

The correct answer is almost always "Yes" and "Windows (CR LF)". The reason is that line endings in source files should almost always be consistent within the file and source files on Windows should generally have CR LF endings. There are exceptions but if if they applied to you, you would probably know about them.

What is normalize line endings?

Normalizing the line endings is just making sure that all of the line ending characters are consistent. It prevents one line from ending in \r\n and another ending with \r or \n ; the first is the Windows line end pair, while the others are typically used for Mac or Linux files.


1 Answers

I had the same problem and commented out the line

* text=auto

in .gitattributes.

That solved my problem. Hope this helps you or anyone else with the same problem.

From what I understand the * text=auto line is supposed to help with line endings but for some reason a bunch of people running windows seem to have a problem with it.

like image 61
tillmannen Avatar answered Oct 13 '22 14:10

tillmannen