Disable git EOL Conversions

I am trying to get git to not change any line endings whatsoever for any operation. Unfortunately, it seems to do so not matter what. I have reduced it down to the following test case, which has as many different mechanisms for disabling this behavior as I could find.


  • Begin with two machines (Windows computer = A, Linux computer = B)
  • On both machines: git config --global core.autocrlf false
  • On both machines: git config --global core.eol crlf (just in case)

  • Make new repository on A. From an empty folder:
    • git init --shared (then unhide the created .git directory)
    • Make a new file .gitignore in the repository
    • Make a new file .gitattributes in the repository with the single line: * -text
    • git add ., then git commit -m "initial commit" to work around, e.g. this.
    • git branch master_recv
    • Add remotes
  • Make a new file document.txt in the repository containing CRLF
  • Commit: git add -A, then git commit -m "<something>"
  • Note that A's document.txt still contains CRLF (and deleting it and resetting with --hard returns the version still with CRLF)

  • SCP the whole directory to computer B
  • Add a new file new file containing CRLF
  • Commit: git add -A, then git commit -m "<something>"
  • Note that B's document.txt and B's new file both still contain CRLF

  • Pull B's master to A: git pull <remote> master:master_recv
  • A's document.txt has changed to LF. The added file new file also contains LF.

The problem does not occur if B is a Windows machine.


Solution 1:

Inside your project, there should be a .gitattributes file. Most of the time, it should look like below (or this screen-shot):

# Handle line endings automatically for files detected as text 
# and leave all files detected as binary untouched.
* text=auto

# Never modify line endings of our bash scripts
*.sh -crlf

#
# The above will handle all files NOT found below
#
# These files are text and should be normalized (Convert crlf => lf)
*.css           text
*.html          text
*.java          text
*.js            text
*.json          text
*.properties    text
*.txt           text
*.xml           text

# These files are binary and should be left untouched
# (binary is macro for -text -diff)
*.class         binary
*.jar           binary
*.gif           binary
*.jpg           binary
*.png           binary

Change * text=auto to * text=false to disable automatic handling (see screen-shot).

Like this:

enter image description here

If your project doesn't have a .gitattributes file, then the line endings are set by your git configurations. To change your git configurations, do this:

Go to the config file in this directory:

1) C:\ProgramData\Git\config

2) Open up the config file in Notepad++ (or whatever text editor you prefer)

3) Change "autocrlf=" to false.

enter image description here

Solution 2:

One simple solution is:

  • make sure core.autocrlf is set to false for all repos:
    git config --global core.autocrlf false
    • Git 2.16 (Q1 2018) and above, run git add --renormalize .
    • otherwise, re-clone your repo, and check no EOL conversion is done.

If there are conversions automatically done, that mean a .gitattributes core.eol directive is there within the repo.

With Git 2.8+ (March 2016), check if there are still eol transformation with:

git ls-files --eol

Solution 3:

Here is how you do this for a single repo.

Create the file .gitattributes at the root of the repo with this line in it

* -text

That's it. This is a wildcard matching all files, telling git to unset the text attribute. This means git treats all files as binary, and thus does not perform any line-ending conversion.

Solution 4:

I figured it out. It seems that the SCP program was converting the line endings. I noticed this when I tried deliberately making a file with LF endings and then observing that it appeared as CRLF when downloaded.

Since this was the solution for me, I'm accepting this answer, but people of the future should also refer to the other answers for a more general solution.

Solution 5:

From gitattributes(5) Manual Page "Effects" topic

text

This attribute enables and controls end-of-line normalization. When a text file is normalized, its line endings are converted to LF in the repository. To control what line ending style is used in the working directory, use the eol attribute for a single file and the core.eol configuration variable for all text files.

Set

Setting the text attribute on a path enables end-of-line normalization and marks the path as a text file. End-of-line conversion takes place without guessing the content type.

Unset Unsetting the text attribute on a path tells Git not to attempt any end-of-line conversion upon checkin or checkout.

core.autocrlf in new (1.7.2+) Git not used, core.eol and correct setting|unsetting of text-attribute considered as more reliable way