gpt4 book ai didi

git - git-merge 是否可以忽略行尾差异?

转载 作者:IT王子 更新时间:2023-10-29 01:18:52 32 4
gpt4 key购买 nike

git merge 是否可以忽略行尾差异?

也许我问错了问题……但是:

我尝试了 uisng config.crlf 输入,但事情变得有点困惑和失控,特别是当我事后应用它时。

一方面,事后应用此配置似乎不会影响在应用此选项之前提交到存储库的文件。另一件事是突然之间所有提交现在都会导致大量关于 CRLF 被转换为 LF 的恼人警告消息。

老实说,我不太关心使用什么行尾,我个人更喜欢 Unix 风格 \n,但无论如何。我所关心的只是让 git merge 更聪明一点,并忽略行尾的差异。

有时我有两个相同的文件,但 git 会将它们标记为冲突(并且冲突是整个文件)仅仅是因为它们使用了不同的行结束符。

更新:

我发现 git diff 接受一个 --ignore-space-at-eol 选项,是否可以让 git merge 也使用这个选项吗?

最佳答案

2013 年更新:

最近的 git 版本授权使用 merge 策略 recursive 和策略 option (-X):

但是使用“-Xignore-space-changeis also a possibility

  • Fab-V 提到 below :
    git merge master -s recursive -X renormalize

jakub.gcomments 策略也适用于 cherry-picking:

git cherry-pick abcd123456 --strategy=recursive --strategy-option=renormalize 

这比 ignore-all-space 效果好得多。


在 Git 2.29(2020 年第 4 季度)之前,所有内部使用 merge 递归机制的“merge ”操作都应该遵循 merge.renormalize 配置,但其中很多没有。

参见 commit 00906d6commit 8d55225commit 6f6e7cfcommit fe48efbElijah Newren (newren) (2020 年 8 月 3 日)。
(由 Junio C Hamano -- gitster -- merge 到 commit 4339259 ,2020 年 8 月 10 日)

merge: make merge.renormalize work for all uses of merge machinery

Signed-off-by: Elijah Newren

The 'merge' command is not the only one that does merges; other commands like checkout -m or rebase do as well.

Unfortunately, the only area of the code that checked for the "merge.renormalize" config setting was in builtin/merge.c, meaning it could only affect merges performed by the "merge" command.

Move the handling of this config setting to merge_recursive_config() so that other commands can benefit from it as well.


原始答案(2009 年 5 月)

June 2007 中已经提出了忽略 eol 样式的补丁,但它只涉及 git diff --ignore-space-at-eol,而不涉及 git merge

当时问的问题是:

Should --ignore-space-at-eol be an option to git-merge ?
Merges are where this functionality matters.
What are the semantics of an auto-resolved merge with those options in effect -- are they only used for rename detection, or do we, e.g., not flag conflicts with only whitespace changes ? And if we don't, which version do we accept automatically ?

Julio C Hamano 并不是很热情:

This certainly is tempting, but I suspect that should be left to later rounds.
I suspect that it would introduce a concept of two different kinds of diffs, one to be mechanically processed (i.e. use in merge with "git-merge-recursive", and apply with"git-am"), and another to be inspected by humans to understand.
It often may be useful to munge the input for the latter case, even though the output from comparing munged input files may not be readily usable for mechanical application.

git merge 的一般思路是依赖第三方 merge 工具。

例如,我将 DiffMerge 设置为 Git merge 工具,设置了一个规则集,允许该 merge 工具忽略某些类型文件的 eol。


在 Windows 上安装,使用 MSysGit1.6.3,用于 DOS 或 Git bash session ,使用 DiffMerge 或 KDiff3:

  • 在您的 PATH 中设置一个目录(此处:c:\HOMEWARE\cmd)。
  • 在该目录中添加脚本 merge.sh(您最喜欢的 merge 工具的包装器)

merge .sh:

#!/bin/sh

# Passing the following parameters to mergetool:
# local base remote merge_result

alocal=$1
base=$2
remote=$3
result=$4

if [ -f $base ]
then
#"C:/Program Files/SourceGear/DiffMerge/DiffMerge.exe" "$alocal" "$base" "$remote" -m --result="$result" --title1="Mine" --title2="Merging to: $result" --title3="Theirs"

# for merge respecting eol, KDiff3 is better than DiffMerge (which will always convert LF into CRLF)
# KDiff3 will display eol choices (if Windows: CRLF, if Unix LF)
"C:/Program Files/KDiff3/kdiff3.exe" -m "$base" "$alocal" "$remote" -o "$result"
else
#there is not always a common ancestor: DiffMerge needing 3 files, BASE will be the result
#"C:/Program Files/SourceGear/DiffMerge/DiffMerge.exe" "$alocal" "$result" "$remote" -m --result="$result" --title1="Mine" --title2="Merging to: $result" --title3="Theirs"

# KDiff3 however does know how to merge based on 2 files (not just 3)
"C:/Program Files/KDiff3/kdiff3.exe" -m "$base" "$remote" -o "$result"
fi
  • 为 Git 声明 merge 包装器

Git 配置命令:

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "merge.sh \"$PWD/$LOCAL\" \"$PWD/$BASE\" \"$PWD/$REMOTE\" \"$PWD/$MERGED\"
git config --global mergetool.diffmerge.trustExitCode false
git config --global mergetool.diffmerge.keepBackup false
  • 检查 autoCRLF 是否为假

系统级别的 git 配置:

git config ---system core.autoCRLF=false
  • 测试当两行相同(但它们的 eol 字符相同)时,DiffMerge 或 KDiff3 都会在 merge 期间忽略这些行。

DOS 脚本(注意:dos2unix command comes from here,用于模拟 Unix eol 风格。该命令已复制到本答案开头提到的目录中。):

C:\HOMEWARE\git\test>mkdir test_merge
C:\HOMEWARE\git\test>cd test_merge
C:\HOMEWARE\git\test\test_merge>git init
C:\HOMEWARE\git\test\test_merge>echo a1 > a.txt & echo a2 >> a.txt
C:\HOMEWARE\git\test\test_merge>git add a.txt
C:\HOMEWARE\git\test\test_merge>git commit -m "a.txt, windows eol style"
C:\HOMEWARE\git\test\test_merge>git checkout -b windows
Switched to a new branch 'windows'
C:\HOMEWARE\git\test\test_merge>echo a3 >> a.txt & echo a4 >> a.txt
C:\HOMEWARE\git\test\test_merge>git add a.txt
C:\HOMEWARE\git\test\test_merge>git commit -m "add two lines, windows eol style"
C:\HOMEWARE\git\test\test_merge>git checkout master
C:\HOMEWARE\git\test\test_merge>git checkout -b unix
Switched to a new branch 'unix'
C:\HOMEWARE\git\test\test_merge>echo au3 >> a.txt & echo au4 >> a.txt && echo au5 >> a.txt
C:\HOMEWARE\git\test\test_merge>dos2unix a.txt
Dos2Unix: Processing file a.txt ...
C:\HOMEWARE\git\test\test_merge>git add a.txt
C:\HOMEWARE\git\test\test_merge>git commit -m "add 3 lines, all file unix eol style"
[unix c433a63] add 3 lines, all file unix eol style

C:\HOMEWARE\git\test\test_merge>git merge windows
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.

C:\HOMEWARE\git\test\test_merge>git ls-files -u
100644 39b4c894078a02afb9b1dfeda6f1127c138e38df 1 a.txt
100644 28b3d018872c08b0696764118b76dd3d0b448fca 2 a.txt
100644 3994da66530b4df80189bb198dcfac9b8f2a7b33 3 a.txt

C:\HOMEWARE\git\test\test_merge>git mergetool
Merging the files: a.txt

Normal merge conflict for 'a.txt':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (diffmerge):

此时(点击“返回”),DiffMerge 或 KDiff3 将打开,您将亲眼看到哪些行实际 merge ,哪些行被忽略。

警告:结果文件将始终处于带有 DiffMerge 的 Windows eol 模式 (CRLF)...
KDiff3 提供以一种或另一种方式保存。

关于git - git-merge 是否可以忽略行尾差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/861995/

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