git 合并没有空格冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9776527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:15:02  来源:igfitidea点击:

Merging without whitespace conflicts

gitwhitespacegit-merge

提问by callumacrae

I've got a problem where I've got a large commit which changes about a thousand lines of code, removing whitespace from the end of lines and removing spaces before tabs.

我有一个问题,我有一个大提交,它更改了大约一千行代码,从行尾删除空格并删除制表符之前的空格。

There are also about 50 pull requests for this project, which will all have conflicts when my commit is merged. Is there any way that git can be set up so that when merging future commits, it ignores conflicts where one of them is just a whitespace change?

这个项目也有大约50个pull request,在我的commit合并的时候都会有冲突。有什么方法可以设置 git 以便在合并未来提交时忽略其中一个只是空格更改的冲突?

Modifying git itself or using a third party tool isn't possible, but using a hook is fine.

修改 git 本身或使用第三方工具是不可能的,但使用钩子是可以的。

回答by VonC

 git merge -Xignore-all-space

Or (more precise)

或者(更精确)

 git merge -Xignore-space-change

should be enough to ignore all space related conflicts during the merge.

应该足以在合并期间忽略所有与空间相关的冲突。

See git diff:

git diff

--ignore-space-change

Ignore changes in amount of whitespace.
This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.

忽略空格量的变化。
这会忽略行尾的空格,并认为一个或多个空格字符的所有其他序列是等效的。

--ignore-all-space

Ignore whitespace when comparing lines.
This ignores differences even if one line has whitespace where the other line has none.

比较行时忽略空格。
即使一行有空格而另一行没有空格,这也会忽略差异。

ks1322adds in the comments a good advice:

ks1322在评论中添加了一个很好的建议:

It is worth to merge with --no-commitand review the merge before actual commit.

--no-commit在实际提交之前合并并合并是值得的。



The OP Callum Macraereports that, in that case, the merge proceed uninterrupted, and the trailing spaces contained in the pull request patches are applied to the local files.
However, the OP uses a pre-commit hook which takes care of said trailing spaces.
(I suppose a bit similar to this one, also referenced here).

OP卡勒姆·麦克雷报告说,在这种情况下,合并不中断地进行,并包含在拉请求补丁尾随空格被应用到本地文件。
但是,OP 使用预提交钩子来处理所述尾随空格。
(我想有点类似于这个,也在这里引用)。



The OP's pre-commit hook is referenced here:

此处引用了OP 的预提交挂钩:

In addition to removing trailing whitespace, it removes one to three spaces before tabs (I have tab width set to 4), and adds EOLs.
I've had reports that the code that adds the EOL deletes the file in windows, but haven't been able to replicate it.

除了删除尾随空格之外,它还会删除制表符前的一到三个空格(我将制表符宽度设置为 4),并添加 EOL。
我有报告说添加 EOL 的代码会删除 Windows 中的文件,但无法复制它。