Trying to fix line-endings with git filter-branch, but having no luck

后端 未结 8 736
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 09:02

I have been bitten by the Windows/Linux line-ending issue with git. It seems, via GitHub, MSysGit, and other sources, that the best solution is to have your local repos set

8条回答
  •  死守一世寂寞
    2020-11-22 09:32

    My procedure for dealing with the line endings is as follows (battle tested on many repos):

    When creating a new repo:

    • put .gitattributes in the very first commit along with other typical files as .gitignore and README.md

    When dealing with an existing repo:

    • Create / modify .gitattributes accordingly
    • git commit -a -m "Modified gitattributes"
    • git rm --cached -r . && git reset --hard && git commit -a -m 'Normalize CRLF' -n"
      • -n (--no-verify is to skip pre-commit hooks)
      • I have to do it often enough that I defined it as an alias alias fixCRLF="..."
    • repeat the previous command
      • yep, it's voodoo, but generally I have to run the command twice, first time it normalizes some files, second time even more files. Generally it's probably best to repeat until no new commit is created :)
    • go back-and-forth between the old (just before normalization) and new branch a few times. After switching the branch, sometimes git will find even more files that need to be renormalized!

    In .gitattributes I declare all text files explicitly as having LF EOL since generally Windows tooling is compatible with LF while non-Windows tooling is not compatible with CRLF (even many nodejs command line tools assume LF and hence can change the EOL in your files).

    Contents of .gitattributes

    My .gitattributes usually looks like:

    *.html eol=lf
    *.js   eol=lf
    *.json eol=lf
    *.less eol=lf
    *.md   eol=lf
    *.svg  eol=lf
    *.xml  eol=lf
    

    To figure out what distinct extensions are tracked by git in the current repo, look here

    Issues after normalization

    Once this is done, there's one more common caveat though.

    Say your master is already up-to-date and normalized, and then you checkout outdated-branch. Quite often right after checking out that branch, git marks many files as modified.

    The solution is to do a fake commit (git add -A . && git commit -m 'fake commit') and then git rebase master. After the rebase, the fake commit should go away.

提交回复
热议问题