How to normalize working tree line endings in Git?

I have cloned a repository that had inconsistend line endings. I have added a .gitattributes that sets the text attribute for the files I want to normalize. Now when I commit changes I get the message:

warning: CRLF will be replaced by LF in FILE.
The file will have its original line endings in your working directory.

How can I make git normalize my working copy of the file for me? Preferably I would like git to normalize the entire working tree.

  • git bad config when piping commands
  • How emacs know that file came from git repository?
  • git init Permission denied - /private/etc/apache2
  • How to push from Gitlab to Github with webhooks
  • RStudio GIT on windows using network drives
  • git diff between two different files
  • GitExtensions + Putty via SSH on custom port
  • github no address associated with name
  • What is the significance of missing parameters on the 'git push' command?
  • How can I insert and retrieve a custom field into a .gitconfig file?
  • How to merge old version to head of a branch in git?
  • fatal: Pathspec 'autoload_classmap.php' is in submodule 'module/CocktailMakerModule'
  • 3 Solutions collect form web for “How to normalize working tree line endings in Git?”

    The docs for gitattributes provides the answer:

    $ rm .git/index     # Remove the index to force git to
    $ git reset         # re-scan the working directory
    $ git status        # Show files that will be normalized
    $ git add -u
    $ git add .gitattributes
    $ git commit -m "Introduce end-of-line normalization"
    

    Do this sequence after you have edited .gitattributes.

    The .gitattributes settings will only affect new commits. If this repository has no history published (no others depending on it), you might want to go through the whole history. In Unix/Linux, you can use dos2unix(1) to fix all files in combination with find(1), and using the history rewriting of filter-branch (see the discussion in the git book) you can even clean up the full history of the project.

    Use with utmost care, on a fresh clone. Get in contact with anybody who might have a clone, and advise them what you want to do.

    Alternative approach (differs only in command used)

    Make sure you have no any pending changes in repository:

    $ git status
    $ git stash
    

    Modify .gitattributes so CRLF interpretation will changed:

    $ echo "*.txt  text" >>.gitattributes
    $ git commit -m "Made .txt files a subject to CRLF normalization." -- .gitattributes
    

    Remove data from index and refresh working directory:

    $ git rm --cached -r .
    $ git reset --hard
    

    Review CRLF fixes that Git proposes:

    $ git ls-files --eol
    $ git status
    $ git diff
    

    Agree with Git decision:

    $ git add -u
    $ git commit -m "Normalized CRLF for .txt files"
    

    Reload changes as if clean clone was done:

    $ git rm --cached -r .
    $ git reset --hard
    
    Git Baby is a git and github fan, let's start git clone.