Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

git - git用CRLF代替LF(git replacing LF with CRLF)

Running git on a Windows XP machine, using bash.

(使用bash在Windows XP计算机上运行git。)

I exported my project from SVN, and then cloned a bare repository.

(我从SVN导出了我的项目,然后克隆了一个裸存储库。)

I then pasted the export into the bare repositories directory, and did a:

(然后我将导出粘贴到裸存储库目录中,并执行了:)

git add -A

I then got a list of messages saying:

(然后我得到一条消息列表:)

LF will be replaced by CRLF

(LF将由CRLF取代)

What are the ramifications of this conversion?

(这种转变的后果是什么?)

This is a .NET solution in Visual Studio.

(这是Visual Studio中的.NET解决方案。)

  ask by mrblah translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

These messages are due to incorrect default value of core.autocrlf on Windows.

(这些消息是由于Windows上core.autocrlf默认值不正确造成的。)

The concept of autocrlf is to handle line endings conversions transparently.

(autocrlf的概念是透明地处理行结束转换。)

And it does!

(它确实如此!)

Bad news: value needs to be configured manually.

(坏消息:需要手动配置值。)
Good news: it should only be done ONE time per git installation (per project setting is also possible).

(好消息:每个git安装应该只进行一次(每个项目设置也可以)。)

How autocrlf works :

(autocrlf如何工作 :)

core.autocrlf=true:      core.autocrlf=input:     core.autocrlf=false:

        repo                     repo                     repo
      ^      V                 ^      V                 ^      V
     /                       /                       /        
crlf->lf    lf->crlf     crlf->lf                    /                
   /                       /                       /            

Here crlf = win-style end-of-line marker, lf = unix-style (and mac osx).

(这里crlf = win-style end-of-line marker, lf = unix-style(和mac osx)。)

(pre-osx cr in not affected for any of three options above)

((pre-osx cr不受上述三种选择中的任何一种影响))

When does this warning show up (under Windows)

(该警告何时显示(在Windows下))

autocrlf = true if you have unix-style lf in one of your files (= RARELY),

(- 如果你的一个文件中有unix-style lf (= RARELY),则autocrlf = true ,)
autocrlf = input if you have win-style crlf in one of your files (= almost ALWAYS),

(- autocrlf = input ,如果你有赢风格crlf在文件中的一个(=几乎总是))
autocrlf = false – NEVER!

(- autocrlf = false - 从来没有!)

What does this warning mean

(这个警告意味着什么)

The warning " LF will be replaced by CRLF " says that you (having autocrlf = true ) will lose your unix-style LF after commit-checkout cycle (it will be replaced by windows-style CRLF).

(警告“ LF将被CRLF替换 ”说你(具有autocrlf = true )将在提交 - 结账周期后丢失你的unix风格的LF(它将被windows风格的CRLF取代)。)

Git doesn't expect you to use unix-style LF under windows.

(Git不希望你在windows下使用unix风格的LF。)

The warning " CRLF will be replaced by LF " says that you (having autocrlf = input ) will lose your windows-style CRLF after a commit-checkout cycle (it will be replaced by unix-style LF).

(警告“ CRLF将被LF替换 ”表示您(具有autocrlf = input )将在提交结帐周期后丢失您的Windows样式的CRLF(它将被unix样式的LF替换)。)

Don't use input under windows.

(不要在windows下使用input 。)

Yet another way to show how autocrlf works

(另一种展示autocrlf如何工作的方法)

1) true:             x -> LF -> CRLF
2) input:            x -> LF -> LF
3) false:            x -> x -> x

where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for

(其中x是CRLF(windows风格)或LF(unix风格),箭头代表)

file to commit -> repository -> checked out file

How to fix

(怎么修)

Default value for core.autocrlf is selected during git installation and stored in system-wide gitconfig ( %ProgramFiles(x86)%\git\etc\gitconfig ).

(在git安装期间选择了core.autocrlf默认值,并将其存储在系统范围的gitconfig( %ProgramFiles(x86)%\git\etc\gitconfig )中。)

Also there're (cascading in the following order):

(还有(按以下顺序级联):)

– "global" (per-user) gitconfig located at ~/.gitconfig , yet another

(- 位于~/.gitconfig “global”(每用户) ~/.gitconfig ,另一个)
– "global" (per-user) gitconfig at $XDG_CONFIG_HOME/git/config or $HOME/.config/git/config and

(- $XDG_CONFIG_HOME/git/config global( $XDG_CONFIG_HOME/git/config$HOME/.config/git/config )中的“全局”(每用户)gitconfig)
– "local" (per-repo) gitconfig at .git/config in the working dir.

(- 工作目录中.git/config “local”(per-repo)gitconfig。)

So, write git config core.autocrlf in the working dir to check the currently used value and

(因此,在工作目录中编写git config core.autocrlf来检查当前使用的值和)

– add autocrlf=false to system-wide gitconfig # per-system solution

(- 将autocrlf=false添加到系统范围的gitconfig#per-system解决方案中)
git config --global core.autocrlf false # per-user solution

(- git config --global core.autocrlf false #per-user solution)
git config --local core.autocrlf false # per-project solution

(- git config --local core.autocrlf false #per-project solution)

Warnings

(警告)
git config settings can be overridden by gitattributes settings.

(- git config设置可以被gitattributes设置覆盖。)
crlf -> lf conversion only happens when adding new files, crlf files already existing in the repo aren't affected.

(- crlf -> lf转换仅在添加新文件时发生,回购中已存在的crlf文件不受影响。)

Moral (for Windows):

(道德 (适用于Windows):)
- use core.autocrlf = true if you plan to use this project under Unix as well (and unwilling to configure your editor/IDE to use unix line endings),

(- 如果您计划在Unix下使用此项目(并且不愿意将编辑器/ IDE配置为使用unix行结尾),请使用core.autocrlf = true ,)
- use core.autocrlf = false if you plan to use this project under Windows only (or you have configured your editor/IDE to use unix line endings),

(- 如果您计划仅在Windows下使用此项目(或者您已将编辑器/ IDE配置为使用unix行结尾),请使用core.autocrlf = false ,)
- never use core.autocrlf = input unless you have a good reason to ( eg if you're using unix utilities under windows or if you run into makefiles issues),

(- 永远不要使用core.autocrlf = input除非你有充分的理由( 例如,如果你在windows下使用unix实用程序或者遇到makefile问题),)

PS What to choose when installing git for Windows?

(PS 安装适用于Windows的git时要选择什么?)
If you're not going to use any of your projects under Unix, don't agree with the default first option.

(如果您不打算在Unix下使用任何项目, 请不要同意默认的第一个选项。)

Choose the third one ( Checkout as-is, commit as-is ).

(选择第三个( Checkout as-is,commit as-is )。)

You won't see this message.

(你不会看到这条消息。)

Ever.

(永远。)

PPS My personal preference is configuring the editor/IDE to use Unix-style endings, and setting core.autocrlf to false .

(PPS我个人的偏好是配置编辑器/ IDE以使用Unix风格的结尾,并将core.autocrlf设置为false 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...