How do I force Git to use LF instead of CR+LF under Windows?
I want to force Git to check out files under Windows using just LF
not CR+LF
.
I want to convert all files to have LF
line breaks and keep the LF
in the files.
Remark: I used autocrlf = input
but this just repairs the files when you commit them.
LF
.
Probably I wasn't so clear:
the repository is already using LF
but the files checked out using
Git for Windows ar开发者_如何学编程e using CR+LF
and I want to force Git to get them with LF
:
forcing Unix line endings.
$ git config --list | grep crlf
core.autocrlf=input
The proper way to get LF endings in Windows is to first set core.autocrlf
to false
:
git config --global core.autocrlf false
You need to do this if you are using msysgit, because it sets it to true
in its system settings.
Now git won’t do any line ending normalization. If you want files you check in to be normalized, do this: Set text=auto
in your .gitattributes
for all files:
* text=auto
And set core.eol
to lf
:
git config --global core.eol lf
Now you can also switch single repos to crlf (in the working directory!) by running
git config core.eol crlf
After you have done the configuration, you might want git to normalize all the files in the repo. To do this, go to to the root of your repo and run these commands:
git rm --cached -rf .
git diff --cached --name-only -z | xargs -n 50 -0 git add -f
If you now want git to also normalize the files in your working directory, run these commands:
git ls-files -z | xargs -0 rm
git checkout .
I come back to this question fairly often, though none of its other answers are quite right for me.
That said, the right answer for me is a mixture of the others.
What I find works is the following:
git config --global core.eol lf
git config --global core.autocrlf input
For repos (Git repositories) that were checked out after those global settings were set, everything will be checked out as whatever it is in the repo – hopefully LF
(\n
).
Any CRLF
will be converted to just LF
on check-in (commit).
With an existing repo that you have already checked out – that has the correct line endings in the repo but not your working copy – you can run the following commands to fix it:
git rm -rf --cached .
git reset --hard HEAD
This will delete (rm
) recursively (-r
) without prompt (-f
), all files except those that you have edited (--cached
), from the current directory (.
). The reset
then returns all those files to a state where they have their true line endings (matching what's in the repo).
If you need to fix the line endings of files in a repo, I recommend grabbing an editor that will let you do that in bulk like IntelliJ or Sublime Text, but I'm sure any good one will likely support this.
The OP added in his question:
the files checked out using msysgit are using
CR+LF
and I want to force msysgit to get them withLF
A first simple step would still be in a .gitattributes
file:
# 2010
*.txt -crlf
# 2020
*.txt text eol=lf
(as noted in the comments by grandchild, referring to .gitattributes
End-of-line conversion), to avoid any CRLF
conversion for files with correct eol
.
And I have always recommended git config --global core.autocrlf false
to disable any conversion (which would apply to all versioned files)
See Best practices for cross platform git config?
Since Git 2.16 (Q1 2018), you can use git add --renormalize .
to apply those .gitattributes
settings immediately.
But a second more powerful step involves a gitattribute filter driver and add a smudge step
Whenever you would update your working tree, a script could, only for the files you have specified in the .gitattributes
, force the LF eol
and any other formatting option you want to enforce.
If the "clear
" script doesn't do anything, you will have (after commit) transformed your files, applying exactly the format you need them to follow.
Context
If you
- want to force all users to have LF line endings for text files and
- you cannot ensure that all users change their git config,
you can do that starting with git 2.10. 2.10 or later is required, because 2.10 fixed the behavior of text=auto together with eol=lf. Source.
Solution
Put a .gitattributes
file in the root of your git repository having following contents:
* text=auto eol=lf
Commit it.
Optional tweaks
You can also add an .editorconfig
in the root of your repository to ensure that modern tooling creates new files with the desired line endings.
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
core.autocrlf=input
is the right setting for what you want, but you might have to do a git update-index --refresh
and/or a git reset --hard
for the change to take effect.
With core.autocrlf
set to input
, git will not apply newline-conversion on check-out (so if you have LF in the repo, you'll get LF), but it will make sure that in case you mess up and introduce some CRLFs in the working copy somehow, they won't make their way into the repo.
You can find the solution to this problem at: https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings
Simplified description of how you can solve this problem on windows:
Global settings for line endings The git config core.autocrlf command is used to change how Git handles line endings. It takes a single argument.
On Windows, you simply pass true to the configuration. For example: C:>git config --global core.autocrlf true
Good luck, I hope I helped.
精彩评论