Search code examples
gitgit-mergeline-endings

Extra carriage return at end of every file causing problems in merge


In our git repo our files sometimes end up with additional carriage returns at the end of the line. When I view the files in vim I see;

<?xml version="1.0" encoding="utf-8"?>^M
<Root>^M
    <Child/>^M
</Root>^M

These extra ^M will be on every line in the file. This causes problems when we merge because the other side of the merge will not have the additional ^M and we get many merge conflicts. Passing options to ignore white space to git merge doesn't seem to help, it still conflicts on the whole file.

We use git config core.autoclrf true.

I need two things;

  1. How do a search for all of these ^M in my repo? Such that I can use some git ls-files | grep <filter here> | sed 's/^M//' to get rid of them.
  2. How do I identify how they're being introduced?

Solution

  • The answers to How to normalize working tree line endings in Git? are great for fixing up new commits. (See both the accepted one, and the modern Git 2.16-or-later git add --renormalize trick.)

    To deal more easily with old commits, use the renormalize extended-option: git merge -X renormalize [other options you want if any] branch. This applies your current .gitattributes setting to all three versions of each file that is to be merged.