How to normalize working tree line endings in Git?

后端 未结 6 1998
甜味超标
甜味超标 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 02:14

    For those using v2.16 or better, you can simply use:

    git add --renormalize .  # Update index with renormalized files
    git status               # Show the files that will be normalized
    git commit -m "Introduce end-of-line normalization"
    

    These directions are straight out of the gitattributes. For older versions, the docs (prior to v2.12) provide a different 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.

    Update

    It appears some users have had trouble with the above instructions. Updated docs for gitattributes (2.12 to 2.14) shows a new set of instructions (after editing the .gitattributes files):

    git read-tree --empty   # Clean index, force re-scan of working directory
    git add .
    git status        # Show files that will be normalized
    git commit -m "Introduce end-of-line normalization"
    

    Thanks to @vossad01 for pointing this out.

    Also, with either solution the files in your working copy still retain their old line endings. If you want to update them, make sure your working tree is clean and use:

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

    Now the line endings will be correct in your working tree.

提交回复
热议问题