Thursday, May 29, 2014

warning: CRLF will be replaced by LF in .gitignore.

Warnings

If you get the following warnings...


$ git status

warning: CRLF will be replaced by LF in .gitignore.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in .jshintrc.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in Gruntfile.js.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in package.json.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in .jshintrc.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in Gruntfile.js.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in package.json.
The file will have its original line endings in your working directory.
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

 modified:   .gitignore
 modified:   .jshintrc
 modified:   Gruntfile.js
 modified:   package.json

Untracked files:
  (use "git add ..." to include in what will be committed)

 .bowerrc
 .sass-cache/
 .tmp/
 Gruntfile.js_ORIG
 bower_components/
 build/
 dist/
 frontend/
 karma.conf.js
 test/bower_components/
 test/unit/


... then you probably just cloned a project that was created on the MS Windows platform and git is auto-converting the files to the unix line endings. That's a good thing, assuming you're on a Unix platform.

MS Windows apps typically put append a hidden CR + LF characters on the end of each line of a text file.

Global Settings

Run the following command to git to convert all CRLF line endings to LF before it stores it in the commit:

$ git config --global core.autocrlf input


Run the following command to prevent git from adding updates whose only function was to change the line-ending:

git config --global core.safecrlf true


If you get errors like the following, that means that the git config --global core.safecrlf true command is working.


fatal: CRLF would be replaced by LF in .bowerrc.
fatal: CRLF would be replaced by LF in .gitignore.
fatal: CRLF would be replaced by LF in .jshintrc.


...In which case, you will want to run the dos2unix command below.

Convert Cloned Files

Run the following commands to convert those CRLF line endings to LF and to update the .gitattributes file to auto enforce the LF line endings.

find ./ -type f -exec dos2unix {} \;

Notes

Though I have never had a problem with a dos2unix, it is potentially destructive, so you may want to backup your files before you run this or be more selective on which files you run it against.

Local Settings

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.

echo "# LF (not CRLF) for line endings" > .gitattributes 
echo "* text eol=lf" >> .gitattributes


Notes

If git encounters files with CRLF endings, it will display the following warning messages, which you probably should see.


warning: CRLF will be replaced by LF in .bowerrc.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in Gruntfile.js.
The file will have its original line endings in your working directory.


If you really want to hide those warning messages for this project, you run the following:

$ git config core.safecrlf false


I hear that if you develop in MS Windows then autocrlf=false should keep the endings consistent between local and remote and local git repos. Good luck with that.

Add the following to your .gitattributes file:

$ echo "* text=auto" >>.gitattributes


If you have a pre-existing local repo, then also run these commands to rebuild the index:

$ rm .git/index
$ git reset



References

https://help.github.com/articles/dealing-with-line-endings

No comments:

Post a Comment