Git 用 CRLF 替换 LF

发布于 2024-08-16 07:56:28 字数 118 浏览 7 评论 0原文

在 Windows 计算机上,我使用 git add 添加了一些文件。 我收到警告说:

LF 将被 CRLF 替换

这种转换会产生什么后果?

On a Windows machine, I added some files using git add.
I got warnings saying:

LF will be replaced by CRLF

What are the ramifications of this conversion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

蓬勃野心 2024-08-23 07:56:29

OP的问题与Windows相关,我无法在不进入目录甚至运行文件的情况下使用其他问题 Notepad++ 作为管理员不起作用...

所以必须走这条路:

cd "C:\Program Files (x86)\Git\etc"
git config --global core.autocrlf false

The OP's question is Windows-related and I could not use others without going to the directory or even running file in Notepad++ as administrator did not work...

So had to go this route:

cd "C:\Program Files (x86)\Git\etc"
git config --global core.autocrlf false
末蓝 2024-08-23 07:56:29

许多文本编辑器允许您更改为LF。请参阅下面的 Atom 说明。它简单而明确。


点击右下角的CRLF

在此处输入图像描述

在顶部下拉列表中选择 LF

在此处输入图像描述

Many text editors allow you to change to LF. See the Atom instructions below. It is simple and explicit.


Click CRLF on the bottom right:

Enter image description here

Select LF in dropdown on top:

Enter image description here

梦冥 2024-08-23 07:56:29

在 GNU/Linux shell 提示符中, dos2unixunix2dos 命令允许您轻松转换/格式化来自 MS Windows 的文件。

In a GNU/Linux shell prompt, the dos2unix and unix2dos commands allow you to easily convert/format your files coming from MS Windows.

月竹挽风 2024-08-23 07:56:29

CR 和 LF 是一组特殊的字符,有助于格式化我们的代码。

CR (\r) 将光标置于行首,但不创建新行。这就是 macOS 旧版本(目前不适用)的工作原理。

LF (\n) 创建一个新行,但不会将光标置于该行的开头。光标停留在最后一行的末尾。这就是 Unix(包括 macOS)和 Linux 的工作方式。

CRLF (\r\n) 创建一个新行并将光标置于新行的开头。这就是我们在 Windows 操作系统中看到的方式。

总结一下:

  1. LF (LINE FEED)
  • 代表换行
  • ,用 \n 表示
  • 在代码中创建一个新行。
  • ASCII 代码为 10。
  • 由 Unix 和其他类似或基于它的操作系统使用,例如 Linux 和现代 macOS。
  1. CR (CARRIAGE RETURN)
  • 代表回车符
  • ,用 \r 表示,
  • 将光标置于行首。
  • ASCII 代码为 13。
  • 在 Mac OS X 之前的 macOS 旧版本中使用。
  1. CRLF (CARRIAGE RETURN AND LINE FEED)
  • 代表回车和换行,
  • 用 \r\n 表示
  • 创建一个新行并将光标放在行首。那条新线。
  • LF 的 ASCII 代码为 10,CR 的 ASCII 代码为 13。
  • 主要用于Windows操作系统。

Git 默认使用 LF。因此,当我们在 Windows 上使用 Git 时,它会抛出类似“CRLF 将被 LF 替换”的警告,并自动将所有 CRLF 转换为 LF,以便代码变得兼容。

注意:别担心……不要将其视为警告,而应将其视为通知消息。

CR and LF are a special set of characters that helps format our code.

CR (\r) puts the cursor at the beginning of a line but doesn't create a new line. This was how legacy versions of macOS (not-applicable today) works.

LF (\n) creates a new line, but it doesn't put the cursor at the beginning of that line. The cursor stays back at the end of the last line. This is how Unix (which includes macOS) and Linux work.

CRLF (\r\n) creates a new line as well as puts the cursor at the beginning of the new line. This is how we see it in Windows OS.

To summarize:

  1. LF (LINE FEED)
  • stands for Line Feed
  • denoted with \n
  • creates a new line in the code
  • The ASCII code is 10.
  • Used by Unix and other OSes like it or based around it, like Linux and modern macOS.
  1. CR (CARRIAGE RETURN)
  • stands for CARRIAGE RETURN
  • denoted with \r
  • puts the cursor on the beginning of a line.
  • The ASCII code is 13.
  • Used by old versions of macOS before Mac OS X.
  1. CRLF (CARRIAGE RETURN AND LINE FEED)
  • stands for CARRIAGE RETURN and LINE FEED
  • denoted with \r\n
  • creates a new line and puts the cursor at the beginning of that new line.
  • The ASCII code is 10 for LF and 13 for CR.
  • Primarily used on Windows OS.

Git uses LF by default. So when we use Git on Windows, it throws a warning like- "CRLF will be replaced by LF" and automatically converts all CRLF into LF, so that code becomes compatible.

NB: Don't worry...see this less as a warning and more as a notification message.

机场等船 2024-08-23 07:56:29

您可以通过创建一个名为 .gitattributes 的文件并将其放入其中来轻松解决:

*.sh text eol=lf

其中“sh”是每个文件的扩展名,需要 LF 而不是 CRLF。我想你的情况是这样吗

You can easily solve by creating a file called .gitattributes putting inside:

*.sh text eol=lf

where "sh" is the extension of each file requires LF instead of CRLF. I suppose is sh i your case

淡莣 2024-08-23 07:56:29

在 Windows 上,我收到此警告是因为我的文件名太长。
将我的文件重命名为更短的文件(并重新启动我的编辑器 VS Code)后,错误消失了。

On windows, I was getting this warning because my files names were too long.
After renaming my files to something shorter (and restarting my editor, VS Code), the error went away.

您的好友蓝忘机已上羡 2024-08-23 07:56:29

要显示配置您的设置的 gitconfig 文件,您可以使用:

git config --list --show-origin | grep core.autocrlf

例如 git bash 在 C:/Program Files/Git/etc/gitconfig 中有自己的“全局”git 配置文件,但经常被忽视。

To show show the gitconfig file where your setting is configured, you can use:

git config --list --show-origin | grep core.autocrlf

For example git bash has its own 'global' git config file in C:/Program Files/Git/etc/gitconfig, and often gets overlooked.

风柔一江水 2024-08-23 07:56:29

我遇到了同样的问题,并正在执行 git add 。 && git reset 正确恢复了所有行结尾。

I had the same issue, and doing git add . && git reset reverted all line endings correctly.

何必那么矫情 2024-08-23 07:56:28

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

autocrlf 的概念是透明地处理行结尾转换。确实如此!

坏消息:该值需要手动配置。

好消息:每个 Git 安装只应执行一次(也可以按项目设置)。

autocrlf 的工作原理

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

     repository               repository               repository
      ^      V                 ^      V                 ^      V
     /        \               /        \               /        \
crlf->lf    lf->crlf     crlf->lf       \             /          \
   /            \           /            \           /            \

这里 crlf = win 风格的行尾标记,lf = unix 风格(自 Mac OS X 起也用于 Mac)。

(osx 之前的 cr 不会受到上述三个选项的影响。)

此警告何时出现(在 Windows 下)?

    – autocrlf = true 如果您的某个文件中有 unix 风格的 lf(= 很少),
    – autocrlf = input 如果您的某个文件中有 win 风格的 crlf(= 几乎总是) ,
    – autocrlf = false – 永远不会!

此警告是什么意思?

警告“LF will be returned by CRLF”表示您(具有 autocrlf=true) 将在提交签出周期后丢失你的 UNIX 风格的 LF(它将被 Windows 风格的 CRLF 取代)。 Git 不希望你在 Windows 下使用 unix 风格的 LF。

警告“CRLF will be returned by LF”表示您(拥有 autocrlf=input)在提交后将丢失 Windows 风格的 CRLF -checkout循环(将被unix风格的LF取代)。不要在 Windows 下使用input

显示 autocrlf 工作原理的另一种方式

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

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

file to commit -> repository -> checked out file

如何修复

core.autocrlf 的默认值是在 Git 安装期间选择的,并存储在系统范围的 gitconfig (\Program Files\Git\etc\gitconfig< Windows 上为 /code>,Linux 上为 /etc/gitconfig)。另外还有(按以下顺序级联):

   – “全局”(每用户)gitconfig 位于 ~/.gitconfig,又一个
    – “全局”(每用户)gitconfig 位于 $XDG_CONFIG_HOME/git/config$HOME/.config/git/config
    – 工作目录中 .git/config 中的“本地”(每个存储库)gitconfig。

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

   – git config --system core.autocrlf false             # 每个系统的解决方案
    – git config --global core.autocrlf false            # 每个用户的解决方案
    – git config --local core.autocrlf false              # 每个项目解决方案

警告

    – git config 设置可以被 gitattributes 设置覆盖。
    – crlf -> lf 转换仅在添加新文件时发生,存储库中已存在的 crlf 文件不受影响。

道德(适用于 Windows):

    - 如果您打算在以下环境中使用此项目,请使用 core.autocrlf = true Unix 也是如此(并且不愿意配置您的编辑器/IDE 以使用 unix 行结尾),
   - 如果您打算仅在 Windows 下使用此项目(或者您已将编辑器/IDE 配置为使用unix 行结尾),
   -永远使用 core.autocrlf = input 除非你有充分的理由(例如< /em> 如果您在 Windows 下使用 unix 实用程序或者遇到 makefile 问题),

PS 安装适用于 Windows 的 Git 时选择什么?

如果您不打算使用任何项目在 Unix 下,同意默认的第一个选项。选择第三个(按原样签出,按原样提交)。您将不会看到此消息。曾经。

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

更新(2022)

自 2018 年以来,git 可以 --renormalize 存储库根据需要修复现有的行结尾。

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

The concept of autocrlf is to handle line endings conversions transparently. And it does!

Bad news: the value needs to be configured manually.

Good news: it should only be done one time per Git installation (per project setting is also possible).

How autocrlf works:

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

     repository               repository               repository
      ^      V                 ^      V                 ^      V
     /        \               /        \               /        \
crlf->lf    lf->crlf     crlf->lf       \             /          \
   /            \           /            \           /            \

Here crlf = win-style end-of-line marker, lf = unix-style (also used on Mac since Mac OS X).

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

When does this warning show up (under Windows)?

    – autocrlf = true if you have unix-style lf in one of your files (= RARELY),
    – autocrlf = input if you have win-style crlf in one of your files (= almost ALWAYS),
    – autocrlf = false – NEVER!

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). Git doesn't expect you to use unix-style LF under Windows.

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). Don't use input under Windows.

Yet another way to show how autocrlf works

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

file to commit -> repository -> checked out file

How to fix

The default value for core.autocrlf is selected during Git installation and stored in system-wide gitconfig (\Program Files\Git\etc\gitconfig on Windows, /etc/gitconfig on Linux). Also there are (cascading in the following order):

   – "global" (per-user) gitconfig located at ~/.gitconfig, yet another
   – "global" (per-user) gitconfig at $XDG_CONFIG_HOME/git/config or $HOME/.config/git/config and
   – "local" (per-repo) gitconfig at .git/config in the working directory.

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

   – git config --system core.autocrlf false            # per-system solution
   – git config --global core.autocrlf false            # per-user solution
   – git config --local core.autocrlf false              # per-project solution

Warnings

    – git config settings can be overridden by gitattributes settings.
    – crlf -> lf conversion only happens when adding new files, crlf files already existing in the repo aren't affected.

Moral (for 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),
    - 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),
    - 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),

PS What to choose when installing Git for Windows?

If you're not going to use any of your projects under Unix, don't agree with the default first option. Choose the third one (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.

Update(2022)

Since 2018, git can --renormalize repo fixing the existing line endings as required.

层林尽染 2024-08-23 07:56:28

Git 具有三种处理行结尾的模式:

# This command will print "true" or "false" or "input"
git config core.autocrlf

您可以通过向上述命令行添加附加参数 truefalse 来设置要使用的模式。

如果 core.autocrlf 设置为 true,则意味着每当您向 Git 存储库添加一个 Git 认为是文本文件的文件时,它都会在存储之前将所有 CRLF 行结尾转换为 LF它在提交中。每当您 git checkout 某些内容时,所有文本文件都会自动将其 LF 行结尾转换为 CRLF 结尾。这允许跨平台开发使用不同行结束样式的项目,而不会造成非常嘈杂的提交,因为每个编辑器都会更改行结束样式,因为行结束样式始终一致为 LF。

这种方便转换的副作用(这就是您看到的警告)是,如果您最初编写的文本文件具有 LF 结尾而不是 CRLF,则它将像往常一样以 LF 存储,但在签出时稍后会有 CRLF 结尾。对于普通文本文件来说,这通常就可以了。在本例中,该警告是“供您参考”,但如果 Git 错误地将二进制文件评估为文本文件,则这是一个重要的警告,因为 Git 会损坏您的二进制文件。

如果 core.autocrlf 设置为 false,则不会执行行结束转换,因此文本文件将按原样签入。只要所有开发人员都在 Linux 上或都在 Windows 上,这通常就可以正常工作。但根据我的经验,我仍然倾向于获取具有混合行结尾的文本文件,这最终会导致问题。

作为一名 Windows 开发人员,我个人的偏好是将设置保持打开状态。

请参阅git-config 获取包含“输入”值的更新信息。

Git has three modes of how it treats line endings:

# This command will print "true" or "false" or "input"
git config core.autocrlf

You can set the mode to use by adding an additional parameter of true or false to the above command line.

If core.autocrlf is set to true, that means that any time you add a file to the Git repository that Git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit. Whenever you git checkout something, all text files automatically will have their LF line endings converted to CRLF endings. This allows development of a project across platforms that use different line-ending styles without commits being very noisy, because each editor changes the line ending style as the line ending style is always consistently LF.

The side effect of this convenient conversion, and this is what the warning you're seeing is about, is that if a text file you authored originally had LF endings instead of CRLF, it will be stored with LF as usual, but when checked out later it will have CRLF endings. For normal text files this is usually just fine. The warning is a "for your information" in this case, but in case Git incorrectly assesses a binary file to be a text file, it is an important warning, because Git would then be corrupting your binary file.

If core.autocrlf is set to false, no line-ending conversion is ever performed, so text files are checked in as-is. This usually works ok, as long as all your developers are either on Linux or all on Windows. But in my experience I still tend to get text files with mixed line endings that end up causing problems.

My personal preference is to leave the setting turned ON, as a Windows developer.

See git-config for updated information that includes the "input" value.

金兰素衣 2024-08-23 07:56:28

如果您已经签出代码,则文件已被索引。更改 Git 设置后,例如运行:

git config --global core.autocrlf input

您应该使用以下命令刷新索引

git rm --cached -r .

并使用以下命令重写 Git 索引

git reset --hard

:这将删除您的本地更改。在执行此操作之前考虑将它们隐藏起来。


来源:配置 Git 处理行结尾

If you already have checked out the code, the files are already indexed. After changing your Git settings, say by running:

git config --global core.autocrlf input

You should refresh the indexes with

git rm --cached -r .

And rewrite the Git index with

git reset --hard

Note: this will remove your local changes. Consider stashing them before you do this.


Source: Configuring Git to Handle Line Endings

输什么也不输骨气 2024-08-23 07:56:28
git config core.autocrlf false
git config core.autocrlf false
挽你眉间 2024-08-23 07:56:28

unix2dosdos2unix 可在 Windows 上使用 Git Bash。您可以使用以下命令来执行 UNIX (LF) → DOS (CRLF) 转换。因此,您不会收到警告。

unix2dos filename

dos2unix -D filename

但是,不要在任何现有的 CRLF 文件上运行此命令,因为这样每隔一行就会得到空换行符。

dos2unix -D filename 并不适用于所有操作系统。请检查此链接以了解兼容性。

如果由于某种原因您需要强制执行该命令,请使用 --force。如果显示无效,则使用 -f

Both unix2dos and dos2unix is available on Windows with Git Bash. You can use the following command to perform UNIX (LF) → DOS (CRLF) conversion. Hence, you will not get the warning.

unix2dos filename

or

dos2unix -D filename

But, don't run this command on any existing CRLF file, because then you will get empty newlines every second line.

dos2unix -D filename will not work with every operating system. Please check this link for compatibility.

If for some reason you need to force the command then use --force. If it says invalid then use -f.

千と千尋 2024-08-23 07:56:28

GitHub 文章在谈论这个话题时,经常会提到在线结尾

我个人使用经常推荐的 core.autocrlf 配置设置的经历非常复杂。

我正在使用 Windows 和 Cygwin,在不同时间处理 Windows 和 Unix 项目。甚至我的 Windows 项目有时也使用 Bash shell 脚本,这需要 Unix (LF) 行结尾。

使用 GitHub 推荐的 Windows 的 core.autocrlf 设置,如果我查看一个 Unix 项目(它在 Cygwin 上完美运行 - 或者也许我正在为我在 Linux 服务器上使用的项目做出贡献),文本文件以 Windows (CRLF) 行结尾签出,从而产生问题。

基本上,对于像我这样的混合环境,将全局 core.autocrlf 设置为任何选项在某些情况下都不会很好地工作。此选项可能在本地(存储库)Git 配置上设置,但即使这样对于包含 Windows 和 Unix 相关内容的项目来说也不够好(例如,我有一个包含一些 Bash 实用程序脚本的 Windows 项目) 。

我发现的最佳选择是创建每个存储库的 .gitattributes 文件。 GitHub 文章 提到了它。

该文章中的示例:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

在我的一个项目存储库中:

* text=auto

*.txt         text eol=lf
*.xml         text eol=lf
*.json        text eol=lf
*.properties  text eol=lf
*.conf        text eol=lf

*.awk  text eol=lf
*.sed  text eol=lf
*.sh   text eol=lf

*.png  binary
*.jpg  binary

*.p12  binary

需要设置的内容较多,但每个项目只需执行一次,并且任何操作系统上的任何贡献者在处理此项目时都应该不会遇到行结尾问题。

A GitHub article on line endings is commonly mentioned when talking about this topic.

My personal experience with using the often recommended core.autocrlf configuration setting was very mixed.

I'm using Windows with Cygwin, dealing with both Windows and Unix projects at different times. Even my Windows projects sometimes use Bash shell scripts, which require Unix (LF) line endings.

Using GitHub's recommended core.autocrlf setting for Windows, if I check out a Unix project (which does work perfectly on Cygwin - or maybe I'm contributing to a project that I use on my Linux server), the text files are checked out with Windows (CRLF) line endings, creating problems.

Basically, for a mixed environment like I have, setting the global core.autocrlf to any of the options will not work well in some cases. This option might be set on a local (repository) Git configuration, but even that wouldn't be good enough for a project that contains both Windows- and Unix-related stuff (e.g., I have a Windows project with some Bash utility scripts).

The best choice I've found is to create per-repository .gitattributes files. The GitHub article mentions it.

Example from that article:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

In one of my project's repository:

* text=auto

*.txt         text eol=lf
*.xml         text eol=lf
*.json        text eol=lf
*.properties  text eol=lf
*.conf        text eol=lf

*.awk  text eol=lf
*.sed  text eol=lf
*.sh   text eol=lf

*.png  binary
*.jpg  binary

*.p12  binary

It's a bit more things to set up, but do it once per project, and any contributor on any OS should have no troubles with line endings when working with this project.

三五鸿雁 2024-08-23 07:56:28

我认为 Basiloungas 的答案很接近,但已经过时了(至少在 Mac 上)。

打开 ~/.gitconfig 文件并将 safecrlf 设置为 false:

[core]
       autocrlf = input
       safecrlf = false

这*将使它明显忽略行尾字符(无论如何,它对我有用)。

I think Basiloungas's answer is close, but out of date (at least on Mac).

Open the ~/.gitconfig file and set safecrlf to false:

[core]
       autocrlf = input
       safecrlf = false

That *will make it ignore the end of line char apparently (it worked for me, anyway).

故乡的云 2024-08-23 07:56:28

在 Vim 中,打开文件(例如: :e YOURFILEENTER),然后

:set noendofline binary
:wq

In Vim, open the file (e.g.: :e YOURFILEENTER), then

:set noendofline binary
:wq
ゞ记忆︶ㄣ 2024-08-23 07:56:28

Windows 中的大多数工具也接受文本文件中的简单 LF。例如,您可以在名为“.editorconfig”的文件中控制 Visual Studio 的行为,其中包含以下示例内容(部分):

 indent_style = space
 indent_size = 2
 end_of_line = lf    <<====
 charset = utf-8

仅原始 Windows 记事本不能与LF一起使用,但是有一些更合适的简单编辑器工具可用!

因此,您也应该在 Windows 中的文本文件中使用 LF。这是我的留言,强烈推荐!没有任何理由在 Windows 中使用 CRLF!

(同样的讨论是在C/++中的包含路径中使用\。它是牛粪便。使用#include带斜杠!,它是C/+ + 标准并且所有 Microsoft 编译器都支持它)。

因此,Git 的正确设置是:

git config core.autocrlf false

我的消息:忘记诸如 dos2unix 和 < a href="https://en.wikipedia.org/wiki/Unix2dos" rel="nofollow noreferrer">unix2dos。向您的团队澄清 LF 适合在 Windows 下使用。

Most of the tools in Windows also accepts a simple LF in text files. For example, you can control the behaviour for Visual Studio in a file named '.editorconfig' with following example content (part):

 indent_style = space
 indent_size = 2
 end_of_line = lf    <<====
 charset = utf-8

Only the original Windows Notepad does not work with LF, but there are some more proper simple editor tools available!

Hence you should use LF in text files in Windows too. This is my message, and it is strongly recommended! There isn’t any reason to use CRLF in windows!

(The same discussion is using \ in include paths in C/++. It is bovine fecal matter. Use #include <pathTo/myheader.h> with slash!, It is the C/++ standard and all Microsoft compilers support it).

Hence the proper setting for Git is:

git config core.autocrlf false

My message: Forget such old thinking programs as dos2unix and unix2dos. Clarify in your team that LF is proper to use under Windows.

梦断已成空 2024-08-23 07:56:28

我也有这个问题。

SVN 不进行任何行结尾转换,因此文件以完整的 CRLF 行结尾提交。如果您随后使用 git-svn 将项目放入 git 中,那么 CRLF 结尾会持续存在到 git 存储库中,这不是 git 期望的状态 - 默认情况下只有 unix/linux (LF) 行结尾 然后,当您在 Windows 上签出

文件时,autocrlf 转换会保持文件完好无损(因为它们已经具有当前平台的正确结尾),但是决定与签入文件是否存在差异的过程会执行比较之前的反向转换,导致将签出文件中的 LF 与存储库中意外的 CRLF 进行比较。

据我所知,您的选择是:

  1. 在不使用 git-svn 的情况下将代码重新导入到新的 git 存储库中,这意味着行结尾会在初始 git commit --all
  2. 设置 autocrlf 中进行转换为 false,并忽略行结尾不是 git 首选样式的事实在
  3. 关闭 autocrlf 的情况下检查文件,修复所有行结尾,重新检查所有内容,然后再次打开它。
  4. 重写存储库的历史记录,以便原始提交不再包含 git 不期望的 CRLF。 (有关历史重写的常见警告适用)

脚注:如果您选择选项#2,那么我的经验是,某些辅助工具(rebase、补丁等)无法处理 CRLF 文件,您将结束迟早会出现混合有 CRLF 和 LF(行结尾不一致)的文件。我不知道有什么办法可以两全其美。

I had this problem too.

SVN doesn't do any line ending conversion, so files are committed with CRLF line endings intact. If you then use git-svn to put the project into git then the CRLF endings persist across into the git repository, which is not the state git expects to find itself in - the default being to only have unix/linux (LF) line endings checked in.

When you then check out the files on windows, the autocrlf conversion leaves the files intact (as they already have the correct endings for the current platform), however the process that decides whether there is a difference with the checked in files performs the reverse conversion before comparing, resulting in comparing what it thinks is an LF in the checked out file with an unexpected CRLF in the repository.

As far as I can see your choices are:

  1. Re-import your code into a new git repository without using git-svn, this will mean line endings are converted in the intial git commit --all
  2. Set autocrlf to false, and ignore the fact that the line endings are not in git's preferred style
  3. Check out your files with autocrlf off, fix all the line endings, check everything back in, and turn it back on again.
  4. Rewrite your repository's history so that the original commit no longer contains the CRLF that git wasn't expecting. (The usual caveats about history rewriting apply)

Footnote: if you choose option #2 then my experience is that some of the ancillary tools (rebase, patch etc) do not cope with CRLF files and you will end up sooner or later with files with a mix of CRLF and LF (inconsistent line endings). I know of no way of getting the best of both.

不必你懂 2024-08-23 07:56:28

从 ~/.gitattributes 文件中删除以下内容,

* text=auto

将首先阻止 Git 检查行结尾。

Removing the below from the ~/.gitattributes file,

* text=auto

will prevent Git from checking line-endings in the first place.

記憶穿過時間隧道 2024-08-23 07:56:28

如何让 Git 忽略不同的行结尾

http://www.rtuin.nl/2013/02/how-to-make-git-ignore- Different-line-endings/< /a>(不工作)

您可以完全禁用 CRLF 行为,或者通过更改 .gitattributes 文件中的条目来按文件类型禁用 CRLF 行为。就我而言,我这样说:

  • -crlf
    这告诉 Git 忽略所有文件的行结尾。并且不会更改工作目录中的文件。即使您将 core.autocrlf 设置为 true、false 或 input。
echo "* -crlf" > .gitattributes

在单独的提交上执行此操作,否则当您进行单个更改时,Git 可能仍然会看到整个文件已修改(取决于您是否更改了 autocrlf 选项)。

这个确实有效。 Git 将尊重混合行结尾项目中的行结尾,并且不会警告您。

How to make Git ignore different line endings

http://www.rtuin.nl/2013/02/how-to-make-git-ignore-different-line-endings/ (Not working)

You can disable the CRLF behaviour completely, or per filetype by changing entries in your .gitattributes file. In my case, I put this:

  • -crlf
    This tells Git to ignore the line endings for all files. And does not change the files in your working directory. Even if you have the core.autocrlf set to true, false, or input.
echo "* -crlf" > .gitattributes

Do this on a separate commit or Git might still see whole files as modified when you make a single change (depending on if you have changed the autocrlf option).

This one really works. Git will respect the line endings in mixed line ending projects and not warn you about them.

强者自强 2024-08-23 07:56:28

我对 Windows 上的 Git 不太了解,但是......

在我看来,Git 正在转换返回格式以匹配运行平台(Windows)的格式。 CRLF 是 Windows 上的默认返回格式,而 LF 是大多数其他操作系统的默认返回格式。

当代码移动到另一个系统时,返回格式可能会被正确调整。我还认为 Git 足够聪明,可以保持二进制文件完整,而不是尝试将 LF 转换为 CRLF,比如 JPEG 文件。

总之,您可能不需要为这种转换过于担心。但是,如果您将项目存档为 tarball,其他编码员可能会喜欢使用 LF 行终止符而不是 CRLF。取决于您的关心程度(并且取决于您是否使用记事本),如果可以的话,您可能希望将 Git 设置为使用 LF 返回 :)

附录:CR 是 ASCII 代码 13,LF 是 ASCII 代码 10。因此,CRLF 是两个字节,而 LF 是 1 个字节。

I don't know much about Git on Windows, but...

It appears to me that Git is converting the return format to match that of the running platform (Windows). CRLF is the default return format on Windows, while LF is the default return format for most other OSes.

Chances are, the return format will be adjusted properly when the code is moved to another system. I also reckon Git is smart enough to keep binary files intact rather than trying to convert LFs to CRLFs in, say, JPEG files.

In summary, you probably don't need to fret too much over this conversion. However, if you go to archive your project as a tarball, fellow coders would probably appreciate having LF line terminators rather than CRLF. Depending on how much you care (and depending on you not using Notepad), you might want to set Git to use LF returns if you can :)

Appendix: CR is ASCII code 13, LF is ASCII code 10. Thus, CRLF is two bytes, while LF is one.

叫嚣ゝ 2024-08-23 07:56:28

它应该是:

警告:(如果您将其签出/或克隆到当前 core.autocrlf 为 true 的另一个文件夹,)LF 将被 CRLF 替换

该文件将在您的(当前)工作目录中保留其原始行结尾。

这张图应该能解释什么意思。

输入图片此处描述

It should read:

warning: (If you check it out/or clone to another folder with your current core.autocrlf being true,)LF will be replaced by CRLF

The file will have its original line endings in your (current) working directory.

This picture should explain what it means.

Enter image description here

何以畏孤独 2024-08-23 07:56:28

确保您已经安装了最新版本的 Git,

就像我在使用 Git(版本 2.7.1)时在之前的答案中所做的那样,git config core.autocrlf false,但它不起作用。

然后在升级 git 时(从 2.7.1 到 2.20.1)它就可以工作了。

Make sure that you have installed the latest version of Git

I did as in a previous answer, git config core.autocrlf false, when using Git (version 2.7.1), but it did not work.

Then it works now when upgrading git (from 2.7.1 to 2.20.1).

情独悲 2024-08-23 07:56:28

在两个不同的操作系统(Linux 和 Windows)中使用“代码”时,CRLF 可能会导致一些问题。

我的 Python 脚本是在 Linux Docker 容器中编写的,然后使用 Windows 进行推送Git Bash。它警告我 LF 将被 CRLF 取代。我没有多想,但后来当我开始编写脚本时,它说:

/usr/bin/env: 'python\r': 没有这样的文件或目录

现在这是一个用于衍生的 \r 。 Windows 使用“CR” - 回车符 - 在“\n”之上作为换行符 - \n\r。这是你可能需要考虑的事情。

CRLF could cause some problem while using your "code" in two different OSes (Linux and Windows).

My Python script was written in a Linux Docker container and then pushed using Windows' Git Bash. It gave me the warning that LF will be replaced by CRLF. I didn't give it much thought, but then when I started the script later, it said:

/usr/bin/env: 'python\r': No such file or directory

Now that is an \r for ramification for you. Windows uses "CR" - carriage return - on top of '\n' as new line character - \n\r. That's something you might have to consider.

其他答案对于一般概念来说非常棒。我遇到了一个问题,更新后,在先前设置中提交的现有存储库上仍然出现警告。

使用 --renormalize 添加有帮助,例如

git add --renormalize 。

来自 文档

" 对所有跟踪的文件强制应用“干净”过程
再次将它们添加到索引中。改了之后这个很有用
core.autocrlf 配置或 text 属性以便更正
添加了错误的 CRLF/LF 行结尾的文件。此选项隐含 -u。”

Other answers are fantastic for the general concept. I ran into a problem where after updating the warning still happened on existing repositories which had commits in previous setting.

Adding with --renormalize helped, e.g.,

git add --renormalize .

From the documentation:

" Apply the "clean" process freshly to all tracked files to forcibly
add them again to the index. This is useful after changing
core.autocrlf configuration or the text attribute in order to correct
files added with wrong CRLF/LF line endings. This option implies -u."

潇烟暮雨 2024-08-23 07:56:28

我刚刚遇到了同样的错误。
在 Windows 10 上安装 NVM NVM 后发生这种情况。

在所有级别设置 autoclrf 都不起作用。

在 CMD 中我使用: git ls-files --eol

i/lf    w/crlf  attr/             src/components/quotes/ExQuoteForm.js
i/lf    w/lf    attr/             src/components/quotes/HighlightedQuote.js

结论:

我创建的文件有不同的结尾。

要更改文件并重置,请执行

git config core.autocrlf false
git rm --cached -r .
git reset --hard

虽然:

在某些项目中,我需要删除存储库并重新启动它。

I just had the same error.
It happened after installing NVM NVM on Windows 10.

Setting the autoclrf in all levels did not worked.

In CMD I used: git ls-files --eol

i/lf    w/crlf  attr/             src/components/quotes/ExQuoteForm.js
i/lf    w/lf    attr/             src/components/quotes/HighlightedQuote.js

Conclusion:

Files I made have the different ending.

To change the files and reset, do

git config core.autocrlf false
git rm --cached -r .
git reset --hard

Although:

In some projects I needed to delete the repository and start it fresh.

白衬杉格子梦 2024-08-23 07:56:28
  1. Notepad++ 中打开文件。
  2. 转至菜单编辑EOL 转换
  3. 单击Windows 格式
  4. 保存文件。
  1. Open the file in Notepad++.
  2. Go to menu EditEOL Conversion.
  3. Click on the Windows Format.
  4. Save the file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文