简体   繁体   中英

git wants to change LF to CRLF on windows - how do I fix this?

In this answer , I found the following chart:

How autocrlf works :

 core.autocrlf=true: core.autocrlf=input: core.autocrlf=false: repo repo repo ^ V ^ V ^ V / \\ / \\ / \\ crlf->lf lf->crl crlf->lf \\ / \\ / \\ / \\ / \\ 

I do most of the development and unit testing locally on a windows box, but the main git repository is on a unix machine, and the code is used on multiple unix machines.

I don't really care how the line endings appear in my windows, but I want very much to not have any CRLF in the repository.

I'm using PyCharm, if that makes any difference.

Here's my git settings:

# ~/.gitconfig :
[user]
    name = ***
    email = ***
[core]
    autocrlf = true
    eol = lf

and

# <path-to-my-project>/.git/config :
[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
    autocrlf = true
[remote "origin"]
    url = git+ssh://***
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

I'd think I have the correct setup, and yet I get the warning: LF will be replaced by CRLF in

My current workaround is to sftp the files to a unix box and commit/push my changes from there, which is really annoying.

How do I fix this?

The easiest way to do this is to use a gitattributes file and specify * text=auto . Git will then look at files, guess whether they are text or binary, and perform automatic line ending conversion for you (provided the file was committed with LF endings originally). You can do this by creating a .gitattributes file with those contents in the repository or writing a custom one in .git/info/attributes . See the gitattributes documentation for more details.

Once you've done that, you can then set core.eol in your config (but not core.autocrlf ) to whatever you want to use. If you set it to crlf or native (the default), Git will check files out in CRLF, and since they're text, it will write them with LF into the repository. If you specify lf , then you'll get LF always, even in the working tree.

The alternative is to specify core.autocrlf (and not core.eol ) to true, which is equivalent to setting your gitattributes to contain * text=auto and core.eol to crlf .

.... or you could ask git to not mess with eol at all and let you handle it. In .gitattributes :

*    -text

That will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM