我的差异包含尾随空格 - 如何摆脱它? [英] My diff contains trailing whitespace - how to get rid of it?

查看:26
本文介绍了我的差异包含尾随空格 - 如何摆脱它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 TextWrangler 中编辑一个 php 文件,在 NetBeans 和 vim 中将行尾设置为 Unix.当我将差异保存到补丁然后尝试应用它时,它会出现空白错误.当我输入 git diff 时,我可以在我的行尾看到 ^M,但是如果我在 vim 中手动删除它们,它会说我的补丁文件已损坏,然后该补丁根本不适用.

I've tried editing a php file in TextWrangler with line endings set to Unix, in NetBeans, and in vim. When I save the diff to a patch and then try to apply it, it gives whitespace errors. When I type git diff I can see ^M at the ends of my lines, but if I manually remove these in vim, it says my patch file is corrupted, and then the patch doesn't apply at all.

我使用以下命令创建补丁:

I create a patch with the following command:

git diff >补丁名称.patch

我通过检查要修补的文件的干净版本并键入来应用它

And I apply it by checking out a clean version of the file to be patched and typing

git apply patchname.patch

如何创建没有空格错误的补丁?我以前创建过补丁,但从未遇到过这个问题.

How can I create this patch without whitespace errors? I've created patches before and never run into this issue.

推荐答案

您确定这些是硬错误吗?默认情况下,git 会警告空格错误,但仍会接受它们.如果它们是硬错误,那么您必须更改了某些设置.您可以将 --whitespace= 标志用于 git apply 以在每次调用的基础上控制它.试试

Are you sure those are hard errors? By default, git will warn about whitespace errors, but will still accept them. If they are hard errors then you must have changed some settings. You can use the --whitespace= flag to git apply to control this on a per-invocation basis. Try

git apply --whitespace=warn patchname.patch

这将强制默认行为,即警告但接受.您还可以使用 --whitespace=nowarn 完全删除警告.

That will force the default behavior, which is to warn but accept. You can also use --whitespace=nowarn to remove the warnings entirely.

控制这个的配置变量是apply.whitespace.

The config variable that controls this is apply.whitespace.

作为参考,此处的空白错误不是您的补丁错误.默认情况下,git 在应用补丁时会抱怨这是一种代码风格的东西.值得注意的是,它不喜欢尾随空格.同样 git diff 将突出显示空白错误(如果您输出到终端并且颜色已打开).默认行为是警告,但无论如何都要接受补丁,因为并非每个项目都热衷于空格.

For reference, the whitespace errors here aren't errors with your patch. It's a code style thing that git will, by default, complain about when applying patches. Notably, it dislikes trailing whitespace. Similarly git diff will highlight whitespace errors (if you're outputting to a terminal and color is on). The default behavior is to warn, but accept the patch anyway, because not every project is fanatical about whitespace.

这篇关于我的差异包含尾随空格 - 如何摆脱它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆