git 我的差异包含尾随空格 - 如何摆脱它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14509950/
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 15:20:08  来源:igfitidea点击:

My diff contains trailing whitespace - how to get rid of it?

gitnetbeanswhitespaceremoving-whitespacetextwrangler

提问by beth

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 diffI can see ^Mat 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.

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

I create a patch with the following command:

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

git diff > patchname.patch

git diff > patchname.patch

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

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

git apply patchname.patch

git apply patchname.patch

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

如何在没有空格错误的情况下创建此补丁?我以前创建过补丁,从未遇到过这个问题。

采纳答案by Lily Ballard

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 applyto control this on a per-invocation basis. Try

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

git apply --whitespace=warn patchname.patch

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

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

The config variable that controls this is apply.whitespace.

控制它的配置变量是apply.whitespace.



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 diffwill 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.

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

回答by zednight

git apply --reject --whitespace=fix mychanges.path

git apply --reject --whitespace=fix mychanges.path

回答by flash

Try patch -p1 < filename.patch

试试 patch -p1 < filename.patch

回答by askalski

the one line solution is:

单行解决方案是:

emacs <filename> -f delete-trailing-whitespace -f save-buffer -f kill-emacs

source: https://wiki.gnome.org/Projects/GnomeShell/Development/WorkingWithPatches

来源:https: //wiki.gnome.org/Projects/GnomeShell/Development/WorkingWithPatches

回答by TRiG

I think the question of how to cope with the whitespace has been adequately answered, but you asked where it came from. You mentioned ^Mat the ends of lines: that's how Git shows Windows line endings. Maybe try running dos2unixon your source files before creating patches, or use an editor which maintains the original line endings.

我认为如何处理空白的问题已经得到了充分的回答,但你问它来自哪里。您^M在行尾提到:这就是 Git 显示 Windows 行尾的方式。也许dos2unix在创建补丁之前尝试运行你的源文件,或者使用一个保持原始行尾的编辑器。