How to normalize working tree line endings in Git?

后端 未结 6 1995
甜味超标
甜味超标 2020-11-28 01:17

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

6条回答
  •  悲&欢浪女
    2020-11-28 01:58

    The * text=auto option in .gitattributes leaves the Git repository in an 'illegal state' if it contains files with CRLF (Windows) line endings which are now marked as text (see https://marc.info/?l=git&m=154484903528621&w=2). The standard renormalize option does not work correctly with the LFS filters, so the instructions in the other answers or for example at https://help.github.com/en/articles/dealing-with-line-endings, do not work correctly. Instead these steps worked for us:

    Situation:

    • On Windows
    • Git repository contained files with both CR and CRLF line endings
    • Added * text=auto to .gitattributes (so not depended on user having set core.crlf=auto on Windows)
    • Also changed the -crlf to -text for LFS tracked files, not sure that is needed.

      1. Create a new branch from the branch with the line ending problem (assuming no uncommitted changes there): git checkout -b feature/doing-stuff-fix-eol
      2. Remove the LFS filters from .gitattributes (replace all 'filter=lfs diff=lfs merge=lfs ' with nothing)
      3. Commit and push: git commit -a -m "Disable LFS filters for EOL fix"
      4. Move to non-git folder
      5. Uninstall LFS globally: git lfs uninstall
      6. Create a new repository clone: git clone -b feature/doing-stuff-fix-eol [remote repository url] fix-eol
      7. Normalize the line endings: git add --renormalize . (note the dot to renormalize all files)
      8. Check only the correct files normalized. It should not include files normally handled by LFS!
      9. Commit and push (save the hash): git commit -m "Fix line endings"
      10. Move to non-git folder
      11. Install LFS globally: git lfs install
      12. Go to original repository clone and pull
      13. Checkout your original branch: git checkout feature/doing-stuff
      14. Cherry pick the eol fix commit and push: git cherry-pick [hash]
      15. Delete the eol branch and push
      16. Delete the eol repository clone (or keep around if you need to fix more branches)

提交回复
热议问题