Skip to content

Instantly share code, notes, and snippets.

@ajdruff
Last active October 13, 2025 19:09
Show Gist options
  • Save ajdruff/16427061a41ca8c08c05992a6c74f59e to your computer and use it in GitHub Desktop.
Save ajdruff/16427061a41ca8c08c05992a6c74f59e to your computer and use it in GitHub Desktop.
Forces all line endings to LF in your git repo.
#####################
#
# Use this with or without the .gitattributes snippet with this Gist
# create a fixle.sh file, paste this in and run it.
# Why do you want this ? Because Git will see diffs between files shared between Linux and Windows due to differences in line ending handling ( Windows uses CRLF and Unix LF)
# This Gist normalizes handling by forcing everything to use Unix style.
#####################
# Fix Line Endings - Force All Line Endings to LF and Not Windows Default CR or CRLF
# Taken largely from: https://help.github.com/articles/dealing-with-line-endings/
# With the exception that we are forcing LF instead of converting to windows style.
#Set LF as your line ending default.
git config --global core.eol lf
#Set autocrlf to false to stop converting between windows style (CRLF) and Unix style (LF)
git config --global core.autocrlf false
#Save your current files in Git, so that none of your work is lost.
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"
# .gitattributes snippet to force users to use same line endings for project.
#
# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto
#
# The above will handle all files NOT found below
# https://help.github.com/articles/dealing-with-line-endings/
# https://github.com/Danimoth/gitattributes/blob/master/Web.gitattributes
# These files are text and should be normalized (Convert crlf => lf)
*.php text
*.css text
*.js text
*.json text
*.htm text
*.html text
*.xml text
*.txt text
*.ini text
*.inc text
*.pl text
*.rb text
*.py text
*.scm text
*.sql text
.htaccess text
*.sh text
# These files are binary and should be left untouched
# (binary is a macro for -text -diff)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.mov binary
*.mp4 binary
*.mp3 binary
*.flv binary
*.fla binary
*.swf binary
*.gz binary
*.zip binary
*.7z binary
*.ttf binary
*.pyc binary
@craigh
Copy link

craigh commented Feb 6, 2020

git add .gitattributes
gave me
fatal: pathspec '.gitattributes' did not match any files

@Peelz
Copy link

Peelz commented Sep 17, 2020

git add .gitattributes
gave me
fatal: pathspec '.gitattributes' did not match any files

Because .gitattributes file not created.

@thdoan
Copy link

thdoan commented Oct 6, 2020

Also add *.svg text

@aborruso
Copy link

Thank you

Copy link

ghost commented Apr 21, 2022

also add *.exe and *.dll And maybe *.c?

Copy link

ghost commented Apr 26, 2022

If you're getting the fatal error shown below, then you need to create the .gitattributes file first, and then put the text in (look above for text) and save it. Then add it with "git add .gitattributes". then git commit then git push
fatal: pathspec '.gitattributes' did not match any files <- error because file does not exist.

@simonnbluebox
Copy link

also add *.exe and *.dll And maybe *.c?

*.exe and *.dll should be in .gitignore, since they're built from source.

@tapirian
Copy link

tapirian commented Jan 4, 2024

I want to force Git to use LF line endings everywhere on the Windows OS, but the .gitattributes file isn't working. I have cleared and reset my Git repository. My editor is vsCode

the .gitattributes file:

* text=auto eol=lf
*.bat text eol=crlf
*.sh text eol=lf

@rosasurfer
Copy link

rosasurfer commented Aug 30, 2025

There is a lot of mis-information here. Generally, you don't need to specify the type of your files in .gitattributes because Git is smart enough to detect the file type by itself. You need to explicitly specify the file type only in one rare scenario:

If Git mis-detects a file type, then you can correct this by specifying the type here manually. However, that's a very rare case (some mixed file content) and for all the mentioned standard file extensions it's fully obsolete. To detect the file type Git operates similar to the Linux file command. Give it a try on a file of your choice and see the results by yourself.

Also be aware that specifying the EOL type for a file in .gitattributes only matters for checkout, i.e. what will be stored in your working directory. It doesn't say anything about how a file will be stored in the repository (and thus distributed to your colleagues).

So, the title of the Gist is wrong. It doesn't force line endings to LF in the repo. It forces line endings to LF in your working directory. In the repo with the advised settings (core.autocrlf = false) the opposite will happen, which usually is not what you want.

Instead, Windows users should always use core.autocrlf = input, which means:

  • convert my files on checkin from CRLF to LF (what gets stored in the repo)
  • don't convert my files on checkout from LF to CRLF (what gets stored in the working tree)

@rofrol
Copy link

rofrol commented Oct 13, 2025

I did this before first commit:

git config core.autocrlf input
git rm --cached -r .
git reset --hard

and it worked

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