20

I am working in IntelliJ 15.0.3 and using Git through Git Bash (to commit and push changes). When I fetch file from remote git repository it contains different line separators (mixed mode or how it's called). I mean that some lines ends with CRLF and some lines ends with LF (the same file).

When I make change in IDEA - file is automatically saved and all line separators are reduced (changed) to IDEA default line separator (LF for me).

And git treats these changes as changes to the file, as a result I commit file with a lot of changes like these:

- some line
+ some line

Because some line [CRLF] was changed to some line [LF].

How to configure Git to ignore this or how to configure IntelliJ IDEA to leave file in this mix-mode? I don't want to commit changes when there are no changes.

ikos23
  • 4,879
  • 10
  • 41
  • 60

4 Answers4

24

While installing git, we will have option to set the checkout as-is and commit as-is.

If that is not set, we can do with the git config.

Following command helps you in doing so.

git config --global core.autocrlf true

As per documentation:

Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true — this converts LF endings into CRLF when you check out code

Mattwmaster58
  • 2,266
  • 3
  • 23
  • 36
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
  • More details: https://stackoverflow.com/questions/9976986/force-lf-eol-in-git-repo-and-working-copy – Vadzim Jun 16 '17 at 20:55
  • 2
    I have added git config --global core.autocrlf true but I still have the issue in intellij whenever I commit files. The intellij editor shows "contents have differences only in line seperators" after commiting. – Srikar Apr 11 '18 at 00:06
  • 2
    Yes it is because you set this after fetching. I think the best you can do is copy you changes to somewhere else, discard the changes with `git checkout -- ` or `git checkout -- *.*`(discard all changes) and fetch again. – WesternGun Nov 28 '18 at 11:35
3

IDEA delegates changes resolution to git.

So you would have to either force desired line breaks in IDEA or to force desired line breaks in git.

In my case autocrlf true was already present on my PC but the working copy was initially copied from collegue's share where it had been checked out with another setting.

A clean checkout would resolve the problem but I had some files already changed and wanted to preserve them. This can be worked around by resetting git index for the whole project or just desired subdirectory.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
1

Try to add below conifguration to your .gitconfig (for unix based OS only)

[core]
    autocrlf = input
    eol = lf

You can also make such changes within terminal:

 git config --global core.eol lf
 git config --global core.autocrlf input
Volodymyr Kret
  • 1,511
  • 15
  • 16
1

At the opposite, if you have an issue with files getting back (while changing branch for example) to CRLF even after changing IntelliJ settings to "Line Separator : LF". Then set "git config --global core.autocrlf false".

I had this issue constantly while working on windows but with a repo that is configured for a Linux machine.

M Matthieu
  • 359
  • 3
  • 10