How can I get Git to truly *ignore* line endings?
Asked Answered
F

1

29

How can I tell Git to truly not care about line endings? To leave them as LF or CRLF, as they originally were, and check them in the same way?

I'm using a Git repository with git-tf to check in to a TFS repository. The rest of my team is using TFS exclusively.

That being the case, sometimes they change line endings without knowing it. For instance, recently a third-party tool normalized its line endings, among other changes. Our repo was updated with these changes, and now the files show as having changes in my directory due to different line endings.

What I really want, for this particular repository, is to have Git pretend line-ending changes don't exist. If it's LF, leave it as LF. If it's CRLF, leave it as CRLF.

What setting or combination of settings do I need in order to do this?

Futtock answered 19/3, 2014 at 17:10 Comment(1)
Did you find a solution to this? It's almost a year and I am in same boat. I am working in git and using git-tf for my workflow. I commit to TFS once in a while (git tfs rcheckin). This screws up the line endings in TFS (converts to LF).Donelladonelle
F
26

For future reference: the most stable way to implement this, is using a .gitattributes file that is committed in the root of the git repository.

This file should contain the following:

# no eol conversions!
* -text

This means the following:

  • [*]: this is a file filter and matches any file
  • [-text]: do not attempt to do any end-of-line conversion upon check-in and check-out

Note: using "text=auto" would mean: use the native end-of-line format on the checked out file (for anything that looks like text) and store it as "LF" internally.

This is robust because everyone that clones the repository will be using the same settings. (This is not the case when using core.autocrlf.)

See also Git documentation on gitattributes (effects: text).

Folder answered 3/6, 2015 at 21:45 Comment(4)
OMFG! Why does not by default we have that in TFS! Thank you so much.Chatoyant
This doesn't work for me (SourceTree GUI on macOS). I tried to match any .c file and make Git ignore the line-endings. Checked in .gitattributes file with *.c -text or **/*.c -text, both don't work. After I change line-endings in a .c file Git still picks it up. Also tried a fresh checkout of the repo. Any ideas?Hardner
@Hardner The described change tells git to not manipulate the line-endings. Git leaves the line-endings as they were committed. So it does not have the effect that you expect.Folder
Oh, I see. I thought my and OP's issue are similar, only the in the other direction (line-endings changed in repo vs locally).Hardner

© 2022 - 2024 — McMap. All rights reserved.