Carriage Returns and Line Feeds will ultimately bite you - Some Git Tips

June 05, 2018 Comment on this post <22> Posted in Linux | Win10

What"s a Carriage and why is it Returning? Carriage Return Line Feed WHAT DOES IT ALL MEAN!?!

The paper on a typewriter rides horizontally on a carriage. The Carriage Return or CR was a non-printable control character that would remix the typewriter to lớn the beginning of the line of text.

Bạn đang xem: Crlf injection

However, a Carriage Return moves the carriage baông xã but doesn"t advance the paper by one line. The carriage moves on the X axes...

And Line Feed or LF is the non-printable control character that turns the Platen (the main rubber cylinder) by one line.

Hence, Carriage Return và Line Feed. Two actions, and for years, two control characters.

Every operating system seems to lớn encode an EOL (over of line) differently. Operating systems in the late 70s all used CR LF together literally because they were interfacing with typewriters/printers on the daily.

Windows uses CRLF because DOS used CRLF because CP/M used CRLF because history.

Mac OS used CR for years until OS X switched lớn LF.

Unix used just a single LF over CRLF và has since the beginning, likely because systems lượt thích Multics started using just LF around 1965. Saving a single byte EVERY LINE was a huge khuyến mãi for both storage và transmission.

Fast-forward to 2018 and it"s maybe time for Windows to lớn also switch to just using LF as the EOL character for Text Files.

Why? For starters, Microsoft finally updated Notepad to handle text files that use LF.


Would such a change be possible? Likely not, it would break the world. Here"s NewLine on .NET Chip Core.

public static String NewLine get Contract.Ensures(Contract.Result() != null);#if !PLATFORM_UNIX return " ";#else return " ";#endif // !PLATFORM_UNIX Regardless, if you regularly use Windows and WSL (Linux on Windows) & Linux together, you"ll want khổng lồ be conscious and aware of CRLF and LF.

Xem thêm: Tải Premiere Full Crack - Download Adobe Premiere Pro Cc 2019 Full Crack

I ran inlớn an interesting situation recently. First, let"s đánh giá what Git does

You can configure .gitattributes khổng lồ tell Git how lớn to lớn treat files, either individually or by extension.


git config --global core.autocrlf trueis set, git will automatically convert files quietly so that they are checked out in an OS-specific way. If you"re on Linux & checkout, you"ll get LF, if you"re on Windows you"ll get CRLF.

Viola on Twitter offers an important clarification:

"gitattributes controls line ending behaviour for a repo, git config (especially with --global) is a per user setting."

99% of the time system and the options available works great.

Except when you are sharing file systems between Linux and Windows. I use Windows 10 and Ubuntu (via WSL) và keep stuff in /mnt/c/github.

However, if I pull from Windows 10 I get CRLF and if I pull from Linux I can LF so then my shell scripts MAY OR MAY NOT WORK while in Ubuntu.

I"ve sầu chosen to lớn create a .gitattributes file that mix both shell scripts and PowerShell scripts lớn LF. This way those scripts can be used & shared và RUN between systems.

*.sh eol=lf*.ps1 eol=lfYou"ve sầu got lots of choices. Again 99% of the time autocrlf is the right thing.

From the GitHub docs:

You"ll notice that files are matched--*.c, *.sln, *.png--, separated by a space, then given a setting--text, text eol=crlf, binary. We"ll go over some possible settings below. text=tự động Git will handle the files in whatever way it thinks is best. This is a good default option. text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. text eol=lf Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows. binary Git will understand that the files specified are not text, và it should not try lớn change them. The binary setting is also an alias for -text -diff.Again, the defaults are probably correct. BUT - if you"re doing weird stuff, sharing files or tệp tin systems across operating systems then you should be aware.

Edward Thomson, a co-maintainer of libgit2, has this to say và points us to lớn his blog post on Line Endings.

I would say this more strongly. Because `core.autocrlf` is configured in a scope that"s per-user, but affects the way the whole repository works, `.gitattributes` should _always_ be used.

If you"re having trouble, it"s probably line endings. Edward"s recommendation is that ALL projects kiểm tra in a .gitattributes.

The key lớn dealing with line endings is to make sure your configuration is committed khổng lồ the repository, using .gitattributes. For most people, this is as simple as creating a tệp tin named .gitattributes at the root of your repository that contains one line:* text=auto

Hope this helps!

I hope Microsoft bought Github so they can fix this CRLF vs LF issue.

— Scott ( June 4, 2018

* Typewriter by Matunos used under Creative Commons

Sponsor: Check out JetBrains Rider: a cross-platsize .NET IDE. Edit, refactor, thử nghiệm và debug ASPhường.NET, .NET Framework, .NET Core, Xamarin or Unity applications. Learn more and tải về a 30-day trial!

Scott is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, & Microsoft employee. He is a failed stand-up comic, a cornrower, & a book author.