Last active
October 7, 2021 11:58
-
-
Save sliekens/3f7c021c41733c3a661399f832393ab9 to your computer and use it in GitHub Desktop.
Configure Git line normalization the right way (using PowerShell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# first install Git 2.16+ | |
# https://git-scm.com/download/win | |
git --version | |
# (optional) reset machine settings because per-machine config makes no sense in team projects | |
# the default (when not set) for core.autocrlf is false (no eol conversions) | |
git config --unset core.autocrlf | |
git config --unset core.safecrlf | |
git config --unset core.eol | |
$root = $(git rev-parse --show-toplevel) | |
Push-Location $root | |
# now create (or replace) a top-level gitattributes file with this magic string | |
Set-Content .gitattributes "* text=auto" | |
# at this point Git is configured to convert all line endings to LF in files that Git thinks¹ contain text | |
# ¹determined using stupid algorithms | |
# if you have a file type that must use Windows line endings then you can add an exception | |
Add-Content .gitattributes ".sln text eol=crlf" -WhatIf | |
# if you need to exclude TEXT files from normalization then unset the `text` attribute | |
Add-Content .gitattributes "donttouchmegit.txt -text" -WhatIf | |
# if you need to exclude non-text files from normalization then unset the `text` AND the `diff` attribute | |
Add-Content .gitattributes "*.bin -text -diff" -WhatIf | |
# or you can use the `binary` attribute which does the same thing | |
Add-Content .gitattributes "*.bin binary" -WhatIf | |
# you can also exclude entire folders from normalization | |
# this is especially useful for css/js libraries that don't have normalized line endings (because you shouldn't care) | |
# create a .gitattributes file in the folder to exclude | |
# configure Git to treat all files in this directory as not-text | |
Set-Content vendor/.gitattributes "* -text" -WhatIf | |
# finally let Git normalize all tracked files | |
git add --renormalize $root | |
git commit -m "Enable EOL normalization" | |
# note: original files in your working dir are not normalized | |
# to normalize your working copy, delete all files and folders (except .git\) and restore them from the repository | |
git rm -rf ** | |
git clean -xdf | |
git checkout -f | |
Pop-Location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment