Skip to content

Instantly share code, notes, and snippets.

@philwilliammee
Last active November 30, 2019 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philwilliammee/041ed30daf993a2bac4f30944f33a932 to your computer and use it in GitHub Desktop.
Save philwilliammee/041ed30daf993a2bac4f30944f33a932 to your computer and use it in GitHub Desktop.
#!/bin/bash
# credits
# https://help.github.com/articles/dealing-with-line-endings/
# https://intellij-support.jetbrains.com/hc/en-us/community/posts/205969644-How-to-Ensure-Always-LF-not-CRLF-on-Windows
echo "set git config to not overide LF on pull (windows only)"
git config --global core.autocrlf false
if [ -e .gitattributes ]
then
echo "Using local .gitattributes file"
else
echo "Adding .gitattributes file"
echo "* text=auto" >> .gitattributes
fi
echo "setting git core config to use lf"
git config --global core.eol lf
echo "normalize all files in git repo"
git add . -u
git commit -m "Saving files before refreshing line endings"
# Remove the index and force Git to rescan the working directory.
rm .git/index
# Rewrite the Git index to pick up all the new line endings.
git reset
# Show the rewritten, normalized files.
git status
# Add all your changed files back, and prepare them for a commit. This is your chance to inspect which files, if any, were unchanged.
git add -u
# It is perfectly safe to see a lot of messages here that read
# "warning: CRLF will be replaced by LF in file."
# Rewrite the .gitattributes file.
git add .gitattributes
# Commit the changes to your repository.
git commit -m "Normalize all the line endings"
read -p "Do You want to normalize all files in working directory to LN (y/n)? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
git ls-files -z | xargs -0 rm
git checkout .
echo "converted all files in working directory to LN"
echo "completed with success"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment