121

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.

Micha Wiedenmann
  • 17,330
  • 20
  • 79
  • 123
imallett
  • 13,288
  • 9
  • 49
  • 115
  • Has `core.autocrlf` *always* been false? It sounds like you have `\n` line endings in your repository already? There is *no* setting to change `\n` in your repository to `\r\n` in your working directory. – Edward Thomson Feb 18 '14 at 00:14
  • It has not always been set (for example when the repo was created originally). However, there shouldn't be any CR line endings in the repo already. Also, again, I don't _want_ any changes to happen whatsoever. – imallett Feb 18 '14 at 01:48
  • I ask because your setup *should* preserve your line endings as CRLF. Could you post some file in your repository *with* its object ID just for my (admittedly probably annoying) edification? – Edward Thomson Feb 18 '14 at 02:11
  • @EdwardThomson how do you mean? The repo isn't public (since the Linux machine isn't). I'll assume you want an example file. See edit. – imallett Feb 18 '14 at 03:23
  • Yep, I agree that that file has CRLF line endings. Can you clarify one thing: you mentioned that the "windows machine's newlines change to CR!" Surely that was a typo, or are you really getting Mac OS 9-style carriage return line endings? – Edward Thomson Feb 18 '14 at 04:29
  • It was a typo; fixed. Note also, on the Linux machine, the file seems to stay CRLF (although I don't do a checkout there, only a change and commit before the subsequent pull). – imallett Feb 18 '14 at 04:35
  • @EdwardThomson See edit. – imallett Feb 18 '14 at 17:33
  • So, I appreciate the edit for clarification, but it still doesn't elucidate the problem. Did you `git add document.txt` on the Linux machine? Did you really `git pull` on the Windows machine or did you SCP the working directory back over to it? – Edward Thomson Feb 18 '14 at 18:04
  • @EdwardThomson see edit. Also, chatp? – imallett Feb 19 '14 at 02:03

5 Answers5

82

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

Top-Master
  • 2,934
  • 1
  • 17
  • 34
Gene
  • 9,441
  • 1
  • 60
  • 54
  • 40
    Why use pictures instead of code tags? Very inconvenient – Clint Oct 16 '17 at 18:43
  • 45
    Because I can add a big red box in the picture to highlight stuff. – Gene Mar 04 '18 at 18:56
  • 28
    Using `* text=false` does not unset text: it leaves text set to the string value false. This has the same effect as leaving text unspecified (not specifically unset). Using `* -text` gives it the special unset setting. Unsetting the text attribute on a path tells git not to attempt any end-of-line conversion upon checkin or checkout. – JustAMartin Mar 08 '18 at 14:01
  • Thank you @Gene for this answer. This has been driving me crazy all day, and this one solved it for me! – LeopardSkinPillBoxHat May 31 '18 at 00:33
  • No problem. Glad I can help. – Gene May 31 '18 at 15:21
  • @Gene, you also could just use code tags and only show the relevant code. The additional context is not useful in any of these examples, there would then not be any need for big red boxes. – Michael Leonard Nov 07 '18 at 18:39
  • 12
    Sorry to say that I can't say thanks for this answer. Cost me half a day to find out that someone had followed the misleading advice. As @JustAMartin pointed out `* text=false` has not effect. Please fix the answer! – Paul B. Nov 23 '18 at 11:18
  • @Gene comments can help you highlight and point out much better whatever solution you need to show, inside a code tag – Salvioner Jun 26 '19 at 14:40
  • Including a .gitattributes file in my project and only adding the `*.sh -crlf` fixed the issue for me. Setting `* text=false` requires you to set up your line ending conversion manually for every kind of file it seems. According to [the git docs](https://git-scm.com/docs/gitattributes#_effects) that is – Rodin10 Sep 04 '19 at 12:14
  • To prevent converting CRLF to LF on my Ubuntu: in project dir: nano .gitattributes `*.java -text` in /etc/ nano gitattributes `* -text autolf=false` Thank you! – Trayan Momkov Jun 08 '20 at 19:15
  • This answer is **not** correct. As noted by @JustAMartin below and in the above comment section, you must use `* -text` and **not** `* text=false`. Until this answer is fixed, I have down voted it. Please fix this answer. – Code Doggo Dec 08 '20 at 22:01
  • It worked 3 years ago when I posted it. Things most likely changed. I'm going to keep the code I know works when I tried it. Thanks for letting others know that code might have changed since the attempt 3 years go. – Gene Dec 09 '20 at 01:32
60

One simple solution is:

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

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
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 4
    JUST DON'T USE `autocrlf` TODAY! unsetted `autocrlf` is equivalent to `false`. You lagged behind the trendy movements in Git – Lazy Badger Feb 17 '14 at 07:19
  • 1
    As above, I had tried this (although not with the global flag), and it didn't work. git version is `1.8.5.2`. – imallett Feb 17 '14 at 16:45
  • @IanMallett "At this point, it appears that the Linux machine still has CRLF": it will until you re-normalize its content, or clone it (like you did on windows) – VonC Feb 18 '14 at 06:29
  • If you're trying to make this for only one repo then simply run `git config core.autocrlf false` -- mentioning this because generally autocrlf should be `true` for windows and `input` for others, but sometimes you need something specific for weird edge cases. In my case, I've downloaded some files and want to record changes I make so I can remember what I did (as opposed to tracking changes to code). I don't know if the line endings are important (and want diffs to make sense against the "real" stuff") so I disabled autocrlf. – Captain Man Aug 31 '20 at 20:31
12

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.

imallett
  • 13,288
  • 9
  • 49
  • 115
7

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.

Max
  • 18,333
  • 4
  • 38
  • 54
4

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

Lazy Badger
  • 87,730
  • 7
  • 72
  • 97
  • 1
    In the `.gitattributes` file, I had explicitly disabled everything as text though, right? Also, I don't see it with an option to make it not do conversion (although `crlf` might have no effect)? – imallett Feb 17 '14 at 16:49
  • 9
    Important thing that gets often confused - to unset `text` and prevent any conversion, you should set .gitattributes to `* -text` and not to `* text=false`. `false` is not a valid value for `text` attribute - git won't recognize it and will fall back to its default autocrlf setting instead. Also, after changing `text` value you have to backup all files from your local repo, do a commit, then restore the files with correct end-of-lines as you need them, and commit them back. Then your end-of-lines won't be modified by git ever again. – JustAMartin Jun 28 '19 at 12:06