Git Auto CRLF (.gitattributes)
To allow for proper collaboration on a Git repository across different platforms, make sure to disable git’s auto CRLF feature and implement typically a .gitattributes file in your repo.
These measures will allow you to configure proper line-endings for each type of filetype extension and prevent git from updating them across multiple platforms (OS) each having their own EOL standard (CR/LF/CRLF).
Info Recommended
core.autocrlfsettings typically vary by OS:
- Windows: use
trueorinput(pair with.gitattributes).- macOS/Linux: use
inputorfalse(pair with.gitattributes).- Mixed teams: use
falseto avoid silent conversions, and enforce line endings in.gitattributes.
As shown in the above hint defining proper line-endings per filetype extension allows a repo to maintain multiple standards for different file types within a single repo e.g., a PowerShell script for Windows, a Bash script for Linux etc.
Notice Avoid the usage of tools such as
dos2unixandunix2dosas they may conflict with git’s handling of line endings and don’t allow for proper granularity.
Verify your current line endings within your repository and their impact
1
2
3
git ls-files --eol
git status
git diff
Update your Git configuration settings
Warning Changing line endings in a repo may cause a large number of changes to show up in
git statusandgit diff. Make sure to review these changes carefully before committing them.
1
2
3
4
5
6
7
# list current configuration settings
git config --list
# add configuration setting auto crlf (use --global in case of privilege
# issues or unintended system-wide changes)
git config --system --add core.autocrlf false
# update configuration setting auto crlf via text-editor
git config --system --edit
Add a .gitattributes file to your repository’s root folder
.gitattributes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Set default behavior to automatically normalize line endings
* text=auto
# Git files should use LF
.gitattributes text eol=lf
.gitignore text eol=lf
# Markdown files should use LF
*.md text eol=lf
*.markdown text eol=lf
# Shell scripts should use LF
*.sh text eol=lf
# Windows scripts should use CRLF
*.ps1 text eol=crlf
*.bat text eol=crlf
*.cmd text eol=crlf
# Json files should use LF
*.json text eol=lf
# Explicitly declare text you want to always be normalized and converted
# to native line endings on checkout
Makefile text eol=lf
Commit your .gitattributes file
1
2
3
4
5
git status
# Make sure no other files are staged,
# if so unstage with `git restore --staged <file>`
git add .gitattributes
git commit -m "ci(.gitattributes): add EOL normalization for file types"
Normalize your current line endings when required
1
git add --renormalize .
Again verify your current line endings within your repository and their impact
1
2
3
git ls-files --eol
git status
git diff
Comments