2

Me and my co-workers have always struggled with this issue. It is quite well documented (some links at the end of the page), but so far I haven't been able to fix this. We code in c# using visual studio 2013.

Whenever we merge two branches, we have loads of "changes", in which a file is fully replaced by an identical one. From what I could read online, I am almost sure it is due to a problem with line endings.

The following answer is the one that helped me the most. The first time I followed the steps, it could only find a single file to be normalised, namely the .gitattributes file. But then I replaced that file by the file below as a first step, and the files expected to be normalised were found. This was all done in my local branch.

# Set the default behaviour, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare the text files you want to always be normalised and converted
# to native line endings on checkout.
*.cs text
*.json text
*.html text
*.csproj text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary

I went through the next steps, and I had the expected messages (below) after typing the command: "git add -u"

message:

warning: CRLF will be replaced by LF in (...)

However, when I switched to the master branch and updated from my local branch, several files were, once again, replaced. I tried to create the same .gitattributes file in the master branch and follow the steps again, but the files that were supposed to be normalised were not found after the "git status" command, and the merge always performed as before, replacing several files by identical ones.

What I am doing wrong?

Stack overflow thread

Official github solution

Community
  • 1
  • 1
ccoutinho
  • 3,308
  • 5
  • 39
  • 47
  • have you tried to update your local git config options – shafeen Feb 25 '16 at 15:10
  • 1
    yes. I tried using this command: "git config --global core.autocrlf true" in both branches before merging, but the outcome was the same – ccoutinho Feb 25 '16 at 15:58
  • If your changes are all committed, try deleting the files from the local filesystem (NOT git rm), then use `git checkout -- files` to get "real" version. – Eris Feb 25 '16 at 17:34
  • @Eris that was not the problem, but I have managed to fix this issue. I will answer my own question with the solution! – ccoutinho Feb 26 '16 at 10:39

1 Answers1

4

The problem was that I didn't Sync (push) my branch of the code with the gitattributes file to the repository, I just committed it. Because I was working locally, I thought it was enough. But it wasn't, and the merge was getting the previous version of the code, without the gitattributes file. This problem was very naive, but since the available documentation I quoted above didn't help, I will post my own tutorial below, that might avoid future github noobs to do the same mistake. My tutorial is mostly based on this thread.

For this tutorial, let's assume the existence of a working branch and a master branch. The idea is to push the gitattributes file to the working branch, download the code for the master branch and update that code with the working branch.

# Add the following content to a file on the root of the repository in the 
# working branch, and name it .gitattributes
----------------------------------------------------------------------------
# Set the default behaviour, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare the text files you want to always be normalised and converted
# to native line endings on checkout.
*.cs text
*.json text
*.html text
*.csproj text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
----------------------------------------------------------------------------

# From the root of the repository in the working branch remove everything from the index 
# (don't forget the '.')
git rm --cached -r .

# Re-add all the deleted files to the index
# (You should get lots of messages like:
#   warning: CRLF will be replaced by LF in <file>.)
git diff --cached --name-only -z | xargs -0 git add

# Commit
git commit -m "Fixed the line ending issue"

# Sync the code

# Switch to the master branch 

# Update (merge) from the working branch
Community
  • 1
  • 1
ccoutinho
  • 3,308
  • 5
  • 39
  • 47