What's the strategy for handling CRLF (carriage return, line feed) with Git?
Asked Answered
C

9

689

I tried committing files with CRLF-ending lines, but it failed.

I spent a whole work day on my Windows computer trying different strategies and was almost drawn to stop trying to use Git and instead try Mercurial.

How to properly handle CRLF line endings?

Cathodoluminescence answered 4/10, 2008 at 20:39 Comment(0)
C
852

Almost four years after asking this question, I have finally found an answer that completely satisfies me!

See the details in github:help's guide to Dealing with line endings.

Git allows you to set the line ending properties for a repo directly using the text attribute in the .gitattributes file. This file is committed into the repo and overrides the core.autocrlf setting, allowing you to ensure consistent behaviour for all users regardless of their git settings.

And thus

The advantage of this is that your end of line configuration now travels with your repository and you don't need to worry about whether or not collaborators have the proper global settings.

Here's an example of a .gitattributes file

# Auto detect text files and perform LF normalization
*        text=auto

*.cs     text diff=csharp
*.java   text diff=java
*.html   text diff=html
*.css    text
*.js     text
*.sql    text

*.csproj text merge=union
*.sln    text merge=union eol=crlf

*.docx   diff=astextplain
*.DOCX   diff=astextplain

# absolute paths are ok, as are globs
/**/postinst* text eol=lf

# paths that don't start with / are treated relative to the .gitattributes folder
relative/path/*.txt text eol=lf

There is a convenient collection of ready to use .gitattributes files for the most popular programming languages. It's useful to get you started.

Once you've created or adjusted your .gitattributes, you should perform a once-and-for-all line endings re-normalization.

Note that the GitHub Desktop app can suggest and create a .gitattributes file after you open your project's Git repo in the app. To try that, click the gear icon (in the upper right corner) > Repository settings ... > Line endings and attributes. You will be asked to add the recommended .gitattributes and if you agree, the app will also perform a normalization of all the files in your repository.

Finally, the Mind the End of Your Line article provides more background and explains how Git has evolved on the matters at hand. I consider this required reading.

You've probably got users in your team who use EGit or JGit (tools like Eclipse and TeamCity use them) to commit their changes. Then you're out of luck, as @gatinueta explained in this answer's comments:

This setting will not satisfy you completely if you have people working with Egit or JGit in your team, since those tools will just ignore .gitattributes and happily check in CRLF files https://bugs.eclipse.org/bugs/show_bug.cgi?id=342372

One trick might be to have them commit their changes in another client, say SourceTree. Our team back then preferred that tool to Eclipse's EGit for many use cases.

Who said software is easy? :-/

Cathodoluminescence answered 1/6, 2012 at 18:56 Comment(17)
How can you see what .gitattributes does GitHub for Windows suggests for your project? I installed GitHub for Windows, started the GUI version and was unable to find any option related with .gitattributes suggestions.Bragg
@JLDiaz, start GitHub for Windows, and in the list of repositories, open the one of your choice. Once (and only once) you have a repository open, click on the tools menu, select settings and you should see on the right what I meant.Cathodoluminescence
@BrechtMachiels If you do * text=auto !eol, do you still need to set autocrlf in your git config?Tater
@BrechtMachiels .gitattributes takes over the autocrlf config for the repo which holds the .gitattributes. Thus someone may want to setup autocrlf on a general basis for repos they use which don't have .gitattributes. But I may have glossed over your !eol part; I don't remember what it does.Cathodoluminescence
For Windows I'm usually inclined to set the global core.autocrlf = false - I prefer LF everywhere, but some of the Windows tools like Visual Studio insist on CRLF endings in certain files (and even mix them in a few..) ; not munging line endings is the safest option. If you know what you are doing, I'd probably use core.autocrlf = input and make exceptions for projects on Windows that you know are sensitive to line endings. As others point out, every decent text editor supports LF endings now. I actually think core.autocrlf = true can probably cause more trouble than it prevents.Taintless
I think * text=auto only applies to files in the root directory [or rather, location of the .gitattributes file directory], is that right?Distinct
@gatinueta To be more specific, it's a JGit issue. Meaning TeamCity, which also uses JGit, straight up ignores .gitattributes.Edson
Collection of the ready-to-use .gitattributes files for the most popular programming languages can be found at github.com/Danimoth/gitattributesHourihan
The link to "Mind the End of Your Line" is broken. Is this the same article? adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-lineDamned
Visual Studio with git in Team Explorer (Not Team City) does seem to support .gitattributes as per this: blogs.msdn.com/b/visualstudioalm/archive/2013/02/26/…Tailback
I also recommend using *.sh text eol=lfRoseberry
@DanielJomphe What if team members are using different platforms? How does a shared .gitattributes file work for someone on windows and another on Linux?Jeu
@DanielJomphe : I'd add one more thing: set core.safecrlf=true. Prevents grief by protesting immediately at commit and not laterGuss
Using * -text in .gitattributes will keep git from changing line endings by default.Stethoscope
I assume that once you perform a once-and-for-all line endings re-normalization, you will lose change history, right?Luxembourg
@AlekDavis You will still have all of your history available with git log. However, git blame will show the commit where line endings change. There are some visual tools that will find the most recent change that isn't only a whitespace change when generating "blames".Mullen
I do not recommend adding * text=auto to your .gitattributes file as it can corrupt your binary files. See this answer for more details.Carrero
M
133

Don't convert line endings. It's not the VCS's job to interpret data -- just store and version it. Every modern text editor can read both kinds of line endings anyway.

Malik answered 4/10, 2008 at 20:42 Comment(20)
Seconded. If you have problems with inconsistent line-endings, the best solution is shout at whoever's using the wrong editor settings until they fix it.Sterol
Daniel: I don't use Git, but that's likely a configuration option in the repo.Malik
Disagree. Native linefeeds on all platforms is a convenience.Tutankhamen
Visual Studio is a PITA when it comes to anything other than CRLF.Maxfield
Git has an option not to convert line-endings it is autocrlf=false and unless you are doing cross-platform development, like say Mono it is best left to false when running under Windows and set to true if you will be developing open-source for Mono.Inaccessible
Without explaining what settings to use to avoid converting line endings, this answer isn't very useful.Forest
The problem with line ending is computing correct diffs. So the answer is wrong and misleading.Bind
If you are working on Windows, you really don't have much choice. You need to have files in CRLF mode on Windows and LF only on Unix-ish platforms.Grisham
The vast majority of people using sftp for unix>win text files use WinSCP... which is a great program with the irritating caveat that it forcibly converts to DOS endings and it cannot be changed. If you're using WinSCP then this is definitely a question that needs to be answered and not blown off with "don't do that."Condensate
@Grisham - on windows, you do not have to have files in crlf, it's just that windows editors may be configured by default to save in this format. Change your editor options. Opening 'lf' eol files is not a problem on windows. (If you're using notepad as your primary editor, then, stop.) In many cases you still need "original" eol, not converted eol; e.g., you can't run bash scripts under cygwin if they are converted to crlf; unit tests may have input/output files that are "corrupted" with this conversion, etc.Hubey
@Condensate - of course you can sftp without the conversion; enable binary conversion, or use scp. by default, sftp does text conversion; by default scp does binary transfer; and sftp (including interactive) has the option to disable conversion.Hubey
Well, I think that John Millikin try to explain, is in case of edit linux files on windows editor, you can change editor behavior, telling somthing like SublimeText does in its preferences: '"default_line_ending": "unix"'Cowage
+1, don't modify EOLs when your team is sharing .bat and .sh files in the same repo.Erechtheus
Why can't we solve all our line ending woes by just defining LF = CRLF in all our tools? It already works like that in so many tools, especially Windows tools. Don't be shy, support both if your project handles newlines, and treat them as equals!Bangka
You just assumed that they will edit the files. However, I'd like to see you try compiling windows line ending files on LinuxMiun
Nope, not if you have committers working on different platforms -- I've had repos get so borked due to differences in EOLs that I've had to nuke them and rebuild from workspaces (and don't say "don't do that", cuz there's often no legit reason to force everyone to standardize). The simplest approach is to pick a single EOL standard (I'd suggest \n so you can save those extra bytes :-) and ensure everyone has their clients configured correctly per-OS. If you have your .gitattributes defined correctly, Git will always do the Right Thing, completely transparently to the dev.Barbbarba
Disagree, If your host machine is Windows, you really don't have much choice.Valley
@RomanStarkov Because it only takes one tool to produce a mixed LF + CRLF file, which brings a whole lot of extra woes; and that's before going into text encoding. The core problem is still the same as it was for decades - there's no such thing as a "plain text file", and we should stop pretending there is. Not going to happen anytime soon, of course - especially with so many file formats going back to text for interoperability (at least XML doesn't pretend like the problem doesn't exist; others aren't such happy little trees :/).Alodie
Bad advice. See what can happen with non native line endings https://mcmap.net/q/11212/-git-replacing-lf-with-crlfGuss
I totally agree. The only reason you ever want any automatic conversions is if you must use some broken tools that cannot store or load the linefeeds correctly. As a convinience feature Git includes a workaround for such tools but it's just a workaround, not the optimal method.Boxthorn
H
92

You almost always want autocrlf=input unless you really know what you are doing.

Some additional context below:

It should be either core.autocrlf=true if you like DOS ending or core.autocrlf=input if you prefer unix-newlines. In both cases, your Git repository will have only LF, which is the Right Thing. The only argument for core.autocrlf=false was that automatic heuristic may incorrectly detect some binary as text and then your tile will be corrupted. So, core.safecrlf option was introduced to warn a user if a irreversable change happens. In fact, there are two possibilities of irreversable changes -- mixed line-ending in text file, in this normalization is desirable, so this warning can be ignored, or (very unlikely) that Git incorrectly detected your binary file as text. Then you need to use attributes to tell Git that this file is binary.

The above paragraph was originally pulled from a thread on gmane.org, but it has since gone down.

Hon answered 10/7, 2009 at 22:36 Comment(14)
Why it is a "Right Thing"?Faulty
core.autocrlf=true is a terrible idea. I've had nothing trouble with that option, plus you have to remember to set it whenever you clone the repository.Caye
Do NOT use autocrlf=true unless you know what you are doing. If you develop in DOS/Win then autocrlf=false will keep the endings the same between remote and local repo's and is the best option in almost every situation.Inaccessible
@Chris - What if your developers have windows and multi-platform projects where some of the multi-platform developers work on OSX or Linux? Shouldn't the best option then be autocrlf=true?Maxfield
That would be the one scenario where that would make sense, but it isn't that common of a scenario, so that's why it doesn't make sense as a default, but on a per-repo basis.Inaccessible
autocrlf=true is good if you're able to init repo in the right way (or strip CRLF/CR at some point). Many editors (especially so under Windows?) produce CRLF and have problems with mixed line feeds. For instance: minimal change can cause conversion of every line. git-diff also has problems when mixing CR with CRLF.Tutankhamen
The kerneltrap.org link is broken, the post can be found here thread.gmane.org/gmane.comp.version-control.git/79726/…Nacelle
@BrettRyan - autocrlf = true is never the safest option.Taintless
I disagree, I work on many projects where windows developers can mess up line endings. I use .gitattributes on all projects now though.Maxfield
Upvoted, with reservations. The introductory paragraph is unhelpful. core.autocrlf=input is the canonical answer. For most use cases, core.autocrlf=true and core.autocrlf=false are overly zealous (...in opposite but equally terrible ways, of course) and hence intrinsically destructive. "Git for Windows" should really have shipped with "Checkout as-is, commit Unix-style line endings" (i.e., core.autocrlf=input) as its default newline strategy. It didn't. So here we here – in frickin' 2015 – still endlessly debating this.Stanchion
@CecilCurry I discovered the other day why not. There are 2 Windows things that don't support Unix-style newlines; Notepad (no problem, don't use it) and Windows Forms text boxes. Visual studio uses these in various places, including... to edit pre- and post-build scripts. So if you use Unix-style newlines, you'll lose all your build script newlines in Visual Studio. Bummer.Debatable
Related: You're just another carriage return line feed in the wallInterrupter
As @BrettRyan mentioned: Wouldn't it be better to use repository wide settings via .gitattributes file ? Was just wondering: it's inconvenient to force every user to take care of his line ending settings on his machine ... Or are there other drawbacks?Valerie
autocrlf is broken; dont use it (keep false) and use gitattributes and safecrlf=true Heres the core git developers discussion on that: git.661346.n2.nabble.com/…Guss
V
65

Two alternative strategies to get consistent about line-endings in mixed environments (Microsoft + Linux + Mac):

A. Global All Repositories Setup

  1. Convert all to one format

    find . -type f -not -path "./.git/*" -exec dos2unix {} \;
    git commit -a -m 'dos2unix conversion'
    
  2. Set core.autocrlf to input on Linux/UNIX or true on MS Windows (repository or global)

    git config --global core.autocrlf input
    
  3. Optionally, set core.safecrlf to true (to stop) or warn (to sing:) to add extra guard comparing if the reversed newline transformation would result in the same file

    git config --global core.safecrlf true
    

B. Or per Repository Setup

  1. Convert all to one format

    find . -type f -not -path "./.git/*" -exec dos2unix {} \;
    git commit -a -m 'dos2unix conversion'
    
  2. Add a .gitattributes file to your repository

    echo "* text=auto" > .gitattributes
    git add .gitattributes
    git commit -m 'adding .gitattributes for unified line-ending'
    

Don't worry about your binary files—Git should be smart enough about them.


More about safecrlf/autocrlf variables

Viand answered 26/6, 2012 at 1:18 Comment(5)
global approach == set and forget for all repos vs. per repo == does not require others to change their global configuration.Viand
dos2unix is an command-line-tool that depending on system you might have to install additionallyViand
They're not exclusive, you can use both approaches at same time. Also, be very careful when using dos2unix - there is a risk of corrupting .git/index and we don't need to apply it to every file. It's better using something like find ./ -name "*.html" and specifying which files you want to apply it to.Burnell
WARNING: before running the find lines, be aware: the dos2unix that comes shiped with Git for Windows has a peculiar (IMO idiotic and dangerous) behaviour, with no arguments: instead of changing to UNIX, it toggles the newline format (DOS <-> UNIX)Longing
And another warning: do not DOS2UNIX your .git folder. Just saying.Spurgeon
C
19

--- UPDATE 3 --- (does not conflict with UPDATE 2)

Considering the case that windows users prefer working on CRLF and linux/mac users prefer working on LF on text files. Providing the answer from the perspective of a repository maintainer:

For me the best strategy(less problems to solve) is: keep all text files with LF inside git repo even if you are working on a windows-only project. Then give the freedom to clients to work on the line-ending style of their preference, provided that they pick a core.autocrlf property value that will respect your strategy (LF on repo) while staging files for commit.

Staging is what many people confuse when trying to understand how newline strategies work. It is essential to undestand the following points before picking the correct value for core.autocrlf property:

  • Adding a text file for commit (staging it) is like copying the file to another place inside .git/ sub-directory with converted line-endings (depending on core.autocrlf value on your client config). All this is done locally.
  • setting core.autocrlf is like providing an answer to the question (exact same question on all OS): "Should git-client:
    • a. convert LF-to-CRLF when checking-out (pulling) the repo changes from the remote?
    • b. convert CRLF-to-LF when adding a file for commit?"
  • and the possible answers (values) are:
    • false: "do none of the above",
    • input: "do only b"
    • true: "do a and and b"
    • note that there is NO "do only a"

Fortunately

  • git client defaults (windows: core.autocrlf: true, linux/mac: core.autocrlf: false) will be compatible with LF-only-repo strategy.
    Meaning: windows clients will by default convert to CRLF when checking-out the repository and convert to LF when adding for commit. And linux clients will by default not do any conversions. This theoretically keeps your repo lf-only.

Unfortunately:

  • There might be GUI clients that do not respect the git core.autocrlf value
  • There might be people that don't use a value to respect your lf-repo strategy. E.g. they use core.autocrlf=false and add a file with CRLF for commit.

To detect ASAP non-lf text files committed by the above clients you can follow what is described on --- update 2 ---: (git grep -I --files-with-matches --perl-regexp '\r' HEAD, on a client compiled using: --with-libpcre flag)

And here is the catch:. I as a repo maintainer keep a git.autocrlf=input so that I can fix any wrongly committed files just by adding them again for commit. And I provide a commit text: "Fixing wrongly committed files".

As far as .gitattributes is concearned. I do not count on it, because there are more ui clients that do not understand it. I only use it to provide hints for text and binary files, and maybe flag some exceptional files that should everywhere keep the same line-endings:

*.java          text !eol # Don't do auto-detection. Treat as text (don't set any eol rule. use client's)
*.jpg           -text     # Don't do auto-detection. Treat as binary
*.sh            text eol=lf # Don't do auto-detection. Treat as text. Checkout and add with eol=lf
*.bat           text eol=crlf # Treat as text. Checkout and add with eol=crlf

Question: But why are we interested at all in newline handling strategy?

Answer: To avoid a single letter change commit, appear as a 5000-line change, just because the client that performed the change auto-converted the full file from crlf to lf (or the opposite) before adding it for commit. This can be rather painful when there is a conflict resolution involved. Or it could in some cases be the cause of unreasonable conflicts.


--- UPDATE 2 ---

The dafaults of git client will work in most cases. Even if you only have windows only clients, linux only clients or both. These are:

  • windows: core.autocrlf=true means convert lines to CRLF on checkout and convert lines to LF when adding files.
  • linux: core.autocrlf=input means don't convert lines on checkout (no need to since files are expected to be committed with LF) and convert lines to LF (if needed) when adding files. (-- update3 -- : Seems that this is false by default, but again it is fine)

The property can be set in different scopes. I would suggest explicitly setting in the --global scope, to avoid some IDE issues described at the end.

git config core.autocrlf
git config --global core.autocrlf
git config --system core.autocrlf
git config --local core.autocrlf
git config --show-origin core.autocrlf

Also I would strongly discourage using on windows git config --global core.autocrlf false (in case you have windows only clients) in contrast to what is proposed to git documentation. Setting to false will commit files with CRLF in the repo. But there is really no reason. You never know whether you will need to share the project with linux users. Plus it's one extra step for each client that joins the project instead of using defaults.

Now for some special cases of files (e.g. *.bat *.sh) which you want them to be checked-out with LF or with CRLF you can use .gitattributes

To sum-up for me the best practice is:

  • Make sure that every non-binary file is committed with LF on git repo (default behaviour).
  • Use this command to make sure that no files are committed with CRLF: git grep -I --files-with-matches --perl-regexp '\r' HEAD (Note: on windows clients works only through git-bash and on linux clients only if compiled using --with-libpcre in ./configure).
  • If you find any such files by executing the above command, correct them. This in involves (at least on linux):
    • set core.autocrlf=input (--- update 3 --)
    • change the file
    • revert the change(file is still shown as changed)
    • commit it
  • Use only the bare minimum .gitattributes
  • Instruct the users to set the core.autocrlf described above to its default values.
  • Do not count 100% on the presence of .gitattributes. git-clients of IDEs may ignore them or treat them differrently.

As said some things can be added in git attributes:

# Always checkout with LF
*.sh            text eol=lf
# Always checkout with CRLF
*.bat           text eol=crlf

I think some other safe options for .gitattributes instead of using auto-detection for binary files:

  • -text (e.g for *.zip or *.jpg files: Will not be treated as text. Thus no line-ending conversions will be attempted. Diff might be possible through conversion programs)
  • text !eol (e.g. for *.java,*.html: Treated as text, but eol style preference is not set. So client setting is used.)
  • -text -diff -merge (e.g for *.hugefile: Not treated as text. No diff/merge possible)

--- PREVIOUS UPDATE ---

One painful example of a client that will commit files wrongly:

netbeans 8.2 (on windows), will wrongly commit all text files with CRLFs, unless you have explicitly set core.autocrlf as global. This contradicts to the standard git client behaviour, and causes lots of problems later, while updating/merging. This is what makes some files appear different (although they are not) even when you revert.
The same behaviour in netbeans happens even if you have added correct .gitattributes to your project.

Using the following command after a commit, will at least help you detect early whether your git repo has line ending issues: git grep -I --files-with-matches --perl-regexp '\r' HEAD

I have spent hours to come up with the best possible use of .gitattributes, to finally realize, that I cannot count on it.
Unfortunately, as long as JGit-based editors exist (which cannot handle .gitattributes correctly), the safe solution is to force LF everywhere even on editor-level.

Use the following anti-CRLF disinfectants.

Crispate answered 21/9, 2017 at 15:17 Comment(2)
I agree with you that this is the best approach, nobody should be using editors without LF support. But be careful with your .gitattributes line, it has unintended conseques in Git < 2.10, see https://mcmap.net/q/11218/-what-will-text-auto-eol-lf-in-gitattributes-doBilliton
Darn it... I have tons of answers of mine advocating for git config --global core.autocrlf false, and recommending to deal with eol in .gitattributes directives only.Bollix
C
12

Using core.autocrlf=false stopped all the files from being marked updated as soon as I checked them out in my Visual Studio 2010 project. The other two members of the development team are also using Windows systems so a mixed environment didn't come into play, yet the default settings that came with the repository always marked all files as updated immediately after cloning.

I guess the bottom line is to find what CRLF setting works for your environment. Especially since in many other repositories on our Linux boxes setting autocrlf = true produces better results.

20+ years later and we're still dealing with line ending disparities between OSes... sad.

Corny answered 16/3, 2011 at 3:10 Comment(6)
@orange80, the disparity is unfortunate, but there's no reason to call it Windows's fault. LF-only makes sense from a minimalist standpoint, perhaps; but CRLF makes more sense based on what CR and LF mean. "Carriage return" means to return to the beginning of the line; "line feed" means to move straight down to the next line, rather than to the beginning of the next line. From a semantic standpoint, Windows is more correct in having both: move back to the beginning (CR) and then down one line (LF).Forest
@Kyralessa "more correct" in still pretending that a computer is a typewriter, which it's not, btw. Maintaining the typewriter analogy doesn't make any sense considering this is not something end-users will ever deal with, and that two characters instead of one is pointless.Hydrokinetic
Late to this party by a few years, but you ignored the fact that CR and LF are cursor positioning tools. "CR" may as well be "Cursor Return" at this point in history. If I wanted the cursor returned to the beginning of the line, I'd tell the application to do that. Otherwise, it needs to stay where I put it.Journalism
Also, if CRLF is "more correct" because a textfile newline really is both a "move one row down" and "move to beginning of line", it would follow that just a CR would cause the text editor to overwrite a line with the following line. I know of no editors which actually support this, meaning that the need to express both CRLF and CR as different things, doesn't really exist.Oosperm
@Oosperm It was very common behavior before DOS, and since Microsoft thinks compatibility is important, it has staid that way ever since. It was also the standard way in the US (as pere ASA) - ISO allowed both CR+LF and LF (so again, DOS was standards compliant); in both cases, since the sixties. Multics (Unix precursor) supported CR for bold/strike. Many applications nowadays (including .NET's "split by lines" features) look for either of the three (lone CR, lone LF, CRLF), and treat each of them as end-line. Many applications are still confused by mixed line-endings in a file, though.Alodie
@Oosperm Unicode has eight new line specifiers that all compliant applications must support. But then again, Unicode no longer pretends that there's something like a "plain text file" - it precisely defines how every character is encoded universally on every system. Sadly, Unicode support is still rather lacking, and seems to still be quite unpopular especially in the Unix world. Programmers usually don't even consider the potential issues until they bite them - I've even seen HTTP libraries that use LF as line separator, even though text internet protocols must use CRLF...Alodie
N
11

Try setting the core.autocrlf configuration option to true. Also have a look at the core.safecrlf option.

Actually it sounds like core.safecrlf might already be set in your repository, because (emphasis mine):

If this is not the case for the current setting of core.autocrlf, git will reject the file.

If this is the case, then you might want to check that your text editor is configured to use line endings consistently. You will likely run into problems if a text file contains a mixture of LF and CRLF line endings.

Finally, I feel that the recommendation to simply "use what you're given" and use LF terminated lines on Windows will cause more problems than it solves. Git has the above options to try to handle line endings in a sensible way, so it makes sense to use them.

Nevernever answered 5/10, 2008 at 3:50 Comment(1)
Wouldn't it be better to use repository wide settings via .gitattributes file ? Was just wondering: it's inconvenient to force every user to take care of his line ending settings on his machine ... Or are there other drawbacks?Valerie
C
9

These are the two options for Windows and Visual Studio users that share code with Mac or Linux users. For an extended explanation, read the gitattributes manual.

* text=auto

In your repo's .gitattributes file add:

*   text=auto

This will normalize all the files with LF line endings in the repo.

And depending on your operating system (core.eol setting), files in the working tree will be normalized to LF for Unix based systems or CRLF for Windows systems.

This is the configuration that Microsoft .NET repos use.

Example:

Hello\r\nWorld

Will be normalized in the repo always as:

Hello\nWorld

On checkout, the working tree in Windows will be converted to:

Hello\r\nWorld

On checkout, the working tree in Mac will be left as:

Hello\nWorld

Note: If your repo already contains files not normalized, git status will show these files as completely modified the next time you make any change on them, and it could be a pain for other users to merge their changes later. See refreshing a repository after changing line endings for more information.

core.autocrlf = true

If text is unspecified in the .gitattributes file, Git uses the core.autocrlf configuration variable to determine if the file should be converted.

For Windows users, git config --global core.autocrlf true is a great option because:

  • Files are normalized to LF line endings only when added to the repo. If there are files not normalized in the repo, this setting will not touch them.
  • All text files are converted to CRLF line endings in the working directory.

The problem with this approach is that:

  • If you are a Windows user with autocrlf = input, you will see a bunch of files with LF line endings. Not a hazard for the rest of the team, because your commits will still be normalized with LF line endings.
  • If you are a Windows user with core.autocrlf = false, you will see a bunch of files with LF line endings and you may introduce files with CRLF line endings into the repo.
  • Most Mac users use autocrlf = input and may get files with CRLF file endings, probably from Windows users with core.autocrlf = false.
Cavill answered 14/2, 2016 at 23:2 Comment(1)
Your command for windows users says git config --global core.autocrl true. You mean git config --global core.autocrlf true.Sharasharai
I
6

This is just a workaround solution:

In normal cases, use the solutions that are shipped with git. These work great in most cases. Force to LF if you share the development on Windows and Unix based systems by setting .gitattributes.

In my case there were >10 programmers developing a project in Windows. This project was checked in with CRLF and there was no option to force to LF.

Some settings were internally written on my machine without any influence on the LF format; thus some files were globally changed to LF on each small file change.

My solution:

Windows-Machines: Let everything as it is. Care about nothing, since you are a default windows 'lone wolf' developer and you have to handle like this: "There is no other system in the wide world, is it?"

Unix-Machines

  1. Add following lines to a config's [alias] section. This command lists all changed (i.e. modified/new) files:

    lc = "!f() { git status --porcelain \
                 | egrep -r \"^(\?| ).\*\\(.[a-zA-Z])*\" \
                 | cut -c 4- ; }; f "
    
  2. Convert all those changed files into dos format:

    unix2dos $(git lc)
    
  3. Optionally ...

    1. Create a git hook for this action to automate this process

    2. Use params and include it and modify the grep function to match only particular filenames, e.g.:

      ... | egrep -r "^(\?| ).*\.(txt|conf)" | ...
      
    3. Feel free to make it even more convenient by using an additional shortcut:

      c2dos = "!f() { unix2dos $(git lc) ; }; f "
      

      ... and fire the converted stuff by typing

      git c2dos
      
Idalla answered 20/3, 2013 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.