Git Line Endings

Macintosh/Linux and Windows computers think differently about line endings. Mac/Linux use only the linefeed character (LF) where as Windows uses both the carriage return and linefeed (CRLF). This difference can be a pain and considered a difference for Git when coming to commit a file.

The solution is to set a global configuration setting.

So, when on Windows use...

git config --global core.autocrlf true

...and if on Mac/Linux use...

git config --global core.autocrlf input

The new, preferred, method of controlling line endings is to populate your .gitattributes file which is then saved with your repository. A quick way to get this done is with the https://docs.github.com/en/github/using-git/configuring-git-to-handle-line-endings website.

The best way to migrating to this approach is to, first, ensure all your files are committed...

git status

Update your .gitattributes file to suit your project then normalise the line ending of your files...

git add --renormalize .

Check to see what files have had their line endings updated...

git status

Finally commit those updates...

git commit -m "Line endings corrected"

Here are some good resources to review:

This is my current .gitattributes template:

* text=auto

# ColdFusion
*.cfm text
*.cfml text
*.cfc text

# Source code
*.bash text eol=lf
*.sh text eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
*.css text
*.htm text diff=html
*.html text diff=html
*.xhtml text diff=html
*.ini text
*.js text
*.json text
*.jsx text
*.less text
*.php text diff=php
*.sass text
*.scm text
*.scss text diff=css
*.sql text
*.xml text

# Docker
Dockerfile text

# Documentation
*.markdown text
*.md text
*.txt text

AUTHORS text
CHANGELOG text
CHANGES text
CONTRIBUTING text
COPYING text
copyright text
*COPYRIGHT* text
INSTALL text
license text
LICENSE text
NEWS text
readme text
*README* text
TODO text

# Templates
*.mustache text
*.njk text
*.twig text
*.vue text

# Configs
*.cnf text
*.conf text
*.config text
.editorconfig text
.env text
.gitattributes text
.gitconfig text
.htaccess text
*.lock text -diff
package-lock.json text -diff
*.toml text
*.yaml text
*.yml text
browserslist text
Makefile text
makefile text

# Graphics
*.ai binary
*.bmp binary
*.eps binary
*.gif binary
*.gifv binary
*.ico binary
*.jng binary
*.jp2 binary
*.jpg binary
*.jpeg binary
*.jpx binary
*.jxr binary
*.pdf binary
*.png binary
*.psb binary
*.psd binary

# SVG treated as an asset (binary) by default.
*.svg text
# If you want to treat it as binary,
# use the following line instead.
# *.svg binary
*.svgz binary
*.tif binary
*.tiff binary
*.wbmp binary
*.webp binary

# Audio
*.m4a binary
*.mid binary
*.midi binary
*.mp3 binary
*.ogg binary
*.ra binary

# Video
*.flv binary
*.m4v binary
*.mov binary
*.mp4 binary
*.mpeg binary
*.mpg binary
*.swc binary
*.swf binary

# Archives
*.7z binary
*.gz binary
*.jar binary
*.rar binary
*.tar binary
*.zip binary

# Fonts
*.ttf binary
*.eot binary
*.otf binary
*.woff binary
*.woff2 binary

# Executables
*.exe binary
*.pyc binary

# Ignore files (like .npmignore or .gitignore)
*.*ignore text