Changing file line endings to LF in mixed unix/windows environments

Sometimes the developers of a project are working in different environments such as Linux, macOS and Windows. A big problem is the different handling of line endings. Windows uses CR/LF and all unix-based systems are using only LF. The code base should always use LF line endings to avoid problems.

So we must tell GIT that we want only LF line endings in our project (the following commands only work in a git repository folder):

git config core.eol lf
git config core.autocrlf input

To set this settings globally for all your git projects, add the --global paramater to the git config command.

You should also create a .gitattributes file in the repository folder and set the line endings for your file types:

# Ensure all project files using correct line endings
Vagrantfile
*.php     eol=lf
*.sh      eol=lf
*.xml     eol=lf
*.java    eol=lf

But this doesn’t affect files with incorrect line encodings already in your reporitory. To fix this, update your repository files with the following commands. Ensure you’ve no uncommited changes before using this.

git rm --cached -r .
git reset --hard
git add .
git commit -m "Normalize line endings"

Now all your files should have correct LF line endings and the changes can be pushed to your remote repository.