Skip to content

Instantly share code, notes, and snippets.

@yuxi-liu-wired
Last active December 4, 2023 03:32
Show Gist options
  • Save yuxi-liu-wired/64424f9c1aa47964f9edb8a8354ed3ad to your computer and use it in GitHub Desktop.
Save yuxi-liu-wired/64424f9c1aa47964f9edb8a8354ed3ad to your computer and use it in GitHub Desktop.
Run this in your local Git repo to convert it from CRLF to LF ending, and make sure it *stay so*.
#!/bin/bash
git config --global core.eol lf
git config --global core.autocrlf input
echo "* text=auto eol=lf" > .gitattributes
# Find files but exclude .git directory
find . -type f ! -path '*/.git/*' | while read -r file; do
# Check if the file is a text file
if file "$file" | grep -q text; then
sed -i 's/\r$//' "$file"
fi
done
@yuxi-liu-wired
Copy link
Author

yuxi-liu-wired commented Dec 4, 2023

Instructions

  • place the script in the root of your Git repository.
  • chmod +x crlf_to_lf.sh
  • ./crlf_to_lf.sh.

How it works

  • Global Git Configuration: Sets Git to handle line endings consistently, using LF (Line Feed) for checkouts and commits.
  • Creates or updates a .gitattributes file in the repository, enforcing the use of LF line endings for all text files.
  • Recursively changes existing CRLF (Carriage Return + Line Feed) endings to LF across the repository, while carefully excluding the .git directory to prevent corruption of Git's internal files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment