gpt4 book ai didi

git - 让 Git 在其 "<<<<<<< HEAD" merge 行上使用 CRLF

转载 作者:IT王子 更新时间:2023-10-29 00:53:21 24 4
gpt4 key购买 nike

是否可以要求 Git 在需要 merge 时在放入文件的行末尾使用 CRLF 而不是仅使用 LF?

Merge using LFs

如果在没有可见 EOL 字符的文本编辑器中解决冲突,如果您通过选择删除,很容易意外地导致这些 LF 被 merge :

Delete by selection

留给你:

LFs sneak into file!

现在两个 LF 已经偷偷进入了您的 CRLF 文件!

显然,一种替代方法是在解决 merge 时更加注意行尾,但我想我会问是否有一种方法可以告诉 Git 对它在此处生成的行使用 CRLF。

最佳答案

Is it possible to ask Git to use CRLF instead of just LF at the end of the lines it puts into a file when it needs merging?

它......实际上是可能的,来自git 2.7.2+ (February 2016) .
而且您无需执行任何操作。

参见 commit 15980de , commit 86efa21 (2016 年 1 月 27 日)作者:Johannes Schindelin (dscho) .
(由 Junio C Hamano -- gitster -- merge 于 commit ab2c107 ,2016 年 2 月 17 日)

merge-file: let conflict markers match end-of-line style of the context

When merging files with CR/LF line endings, the conflict markers should match those, lest the output file has mixed line endings.

This is particularly of interest on Windows, where some editors get really confused by mixed line endings.

The original version of this patch by Beat Bolli respected core.eol, and a subsequent improvement by this developer also respected gitattributes.
This approach was sub-optimal, though: git merge-file was invented as a drop-in replacement for GNU merge and as such has no problem operating outside of any repository at all!

Another problem with the original approach was pointed out by Junio Hamano: legacy repositories might have their text files committed using CR/LF line endings (and core.eol and the gitattributes would give us a false impression there). Therefore, the much superior approach is to simply match the context's line endings, if any.

We actually do not have to look at the entire context at all:

  • if the files are all LF-only, or if they all have CR/LF line endings, it is sufficient to look at just a single line to match that style.
  • And if the line endings are mixed anyway, it is still okay to imitate just a single line's eol: we will just add to the pile of mixed line endings, and there is nothing we can do about that.

So what we do is: we look at the line preceding the conflict, falling back to the line preceding that in case it was the last line and had no line ending, falling back to the first line, first in the first post-image, then the second post-image, and finally the pre-image.
If we find consistent CR/LF (or undecided) end-of-line style, we match that, otherwise we use LF-only line endings for the conflict markers.

Note that while it is true that there have to be at least two lines we can look at (otherwise there would be no conflict), the same is not true for line endings: the three files in question could all consist of a single line without any line ending, each. In this case we fall back to using LF-only.

关于git - 让 Git 在其 "<<<<<<< HEAD" merge 行上使用 CRLF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com