Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: convert carriage return \r to new line \n with git hook?

A fellow coder uses a Windows computer that is putting carriage returns everywhere in our source.

Is there a way to write a git hook that converts all \r\n to \n?

Note I haven't used git hooks before, so a little extra hand-holding might go a long way :)

like image 341
maček Avatar asked Oct 04 '10 20:10

maček


People also ask

How do I change from CRLF to LF in Git?

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.

Does Git change line endings?

autocrlf is set to false on a fresh install of Git, meaning Git won't perform any line ending normalization. Instead, Git will defer to the core. eol setting to decide what line endings should be used; core. eol defaults to native , which means it depends on the OS you're using.

What is Autocrlf in Git?

autocrlf . This is a similar approach to the attributes mechanism: the idea is that a Windows user will set a Git configuration option core. autocrlf=true and their line endings will be converted to Unix style line endings when they add files to the repository.


2 Answers

The simplest thing is to set core.autocrlf to false on The Windows side.
(that way Git won't do any conversion and will keep the eol untouched).

On the unix side, a core.autocrlf set to true could help restore the proper eol.
As mathepic mentions in the comments, and as I described in this SO answer, since Git1.7.2, you can use core.eol (to native), keeping core.autocrlf to false.

Otherwise you can use a text driver or a filter driver with gitattributes files.
No hooks needed.

like image 85
VonC Avatar answered Nov 06 '22 19:11

VonC


If they're using TortoiseGit, there's an option to do this for you, under Git->Config->Auto CRLF convert.

like image 1
Alex Howansky Avatar answered Nov 06 '22 20:11

Alex Howansky