Skip to main content

If you've ever worked in a cross-platform development environment, you've likely encountered line ending issues. These can manifest as warnings in Git, such as:

warning: in the working copy of 'file/path/filename', CRLF will be replaced by LF the next time Git touches it

These warnings arise from differences in line endings between Windows (CRLF) and Unix/Linux (LF) systems. This article will guide you through resolving these issues by configuring your repository to handle line endings consistently.

 

Understanding line endings

Different operating systems use different characters to represent the end of a line:

  • CRLF (Carriage Return + Line Feed): Used by Windows.
  • LF (Line Feed): Used by Unix/Linux and macOS

When working in a cross-platform team, these differences can cause issues with version control, leading to unexpected behaviour and conflicts.

 

Setting up .gitattributes to handle line endings

To resolve these warnings and standardise line endings across your repository.

 

Create or update the .gitattributes file

First, create a file named .gitattributes in the root of your repository. If this file already exists, you'll simply update it.

 

Add line ending settings

Add the following lines to the .gitattributes file to ensure all text files use LF line endings and binary files remain unaffected:

# Set the default behaviour to automatically normalise line endings.
* text=auto
# Explicitly declare text files you want to always be normalised and converted to LF upon checkout.
*.php text eol=lf
*.inc text eol=lf
*.css text eol=lf
*.js text eol=lf
# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary

 

Add and commit the .gitattributes file

Next, add the .gitattributes file to your repository and commit it:

git add .gitattributes
git commit -m "Add .gitattributes to handle line endings"

 

Normalise line endings in your repository

After setting up the .gitattributes file, you need to normalise the line endings in your repository. Run the following commands:

git rm --cached -r .
git reset --hard

The git rm --cached -r . command removes all files from the Git index, while the git reset --hard command resets your working directory to match the latest commit. This effectively re-adds all files to the index and applies the .gitattributes settings.

 

The wrap

By following these steps, you can standardise the line endings in your repository, preventing the annoying warnings and ensuring consistent behaviour across different platforms. Properly managing line endings is a small but crucial part of maintaining a clean and efficient development workflow.

Related articles