为什么 git 关心我文件中的尾随空格?

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

Why does git care about trailing whitespace in my files?

gitwhitespace

提问by Bjorn

What business is it of whatever VCS I'm using to worry about what I put in my files? I don't get what the point here is. Is git version control or syntax checker?

无论我使用什么 VCS 来担心我放入文件中的内容,这有什么关系?我不明白这里的重点是什么。是 git 版本控制还是语法检查器?

采纳答案by Brian Campbell

It is because one of the very common uses for Git is sending patch series via email. Trailing spaces cause trouble in email, and are thus usually stripped out, which means any trailing spaces will be lost in the process of sending the patch via email and applying it. This in turn means that if there are trailing spaces on lines in the repo, but not in the patch being applied, you may get spurious conflicts, or extra changes that weren't intended, when applying a patch.

这是因为 Git 最常见的用途之一是通过电子邮件发送补丁系列。尾随空格会导致电子邮件出现问题,因此通常会被删除,这意味着在通过电子邮件发送补丁并应用它的过程中,任何尾随空格都将丢失。这反过来意味着,如果在 repo 中的行中有尾随空格,但在应用的补丁中没有,则在应用补丁时,您可能会遇到虚假冲突或非预期的额外更改。

This pre-commit hook used to be enabled by default, but is no longer. It seems I have misremembered; it was never deliberately enabled by default. As others have pointed out, this has always been a sample pre-commit hook; it used to be disabled by not giving it the execute bit, but that's something that can get screwed up fairly easily (for instance, running under Cygwin on Windows), so in newer versions of Git (since over a year ago) the samples have been disabled by being named pre-commit.sample. You can delete or move your .git/hooks/pre-committo prevent this hook from executing if you don't like the behavior. You should also update your Git to something more recent, as this has been fixed for quite a while.

这个预提交钩子曾经默认启用,但不再. 我好像记错了;它从未在默认情况下故意启用。正如其他人所指出的,这一直是一个示例预提交挂钩;它曾经通过不给它执行位来禁用,但这是很容易搞砸的东西(例如,在 Windows 上的 Cygwin 下运行),因此在较新版本的 Git(自一年多以前)中,示例有因被命名而被禁用pre-commit.sample.git/hooks/pre-commit如果您不喜欢这种行为,您可以删除或移动您的以防止此挂钩执行。您还应该将 Git 更新到更新的版本,因为这已经修复了很长时间。

回答by hobbs

Git doesn't care at all. The examplepre-commit hook does a whitespace check as a demonstration of how to write a hook, but it's not enabledby default; you have to make it executable for it to do anything.

Git根本不在乎。该例子pre-commit钩子做了空白支票如何写一个钩子演示,但它没有启用默认情况下; 你必须让它可执行才能做任何事情。

回答by Jakub Nar?bski

Why? Because trailing whitespace is easily lost, leading to spurious changes and to not applied patches (because of whitespace change). It is a matter of conforming to programming style.

为什么?因为尾随空白很容易丢失,导致虚假更改和未应用补丁(因为空白更改)。这是符合编程风格的问题

That said it is a question of pre-commithook: you can edit it, or disable it, or configure trailing whitespace (perhaps for some kinds of files only) to be not considered an error.

也就是说,这是一个pre-commit钩子的问题:您可以编辑它,或禁用它,或配置尾随空格(可能仅适用于某些类型的文件)不被视为错误。



The pre-commithook should be disabledby default, but older versions (pre 1.6.0) used to install them turned off by having executable permissions turned off, which might not work on non-UNIX filesystems like FAT; from 1.6.0 they are installed turned off (disabled) by having '.sample' suffix appended.

pre-commit勾应禁用默认的,而是用来安装这些旧版本(1.6.0前)关闭具有可执行权限关闭,这可能会在非UNIX不工作文件系统,如FAT; 从 1.6.0 开始,它们通过附加“.sample”后缀而被关闭(禁用)。

回答by Walinton Cambronero

You can bypass this by using the --no-verify switch, see http://git-scm.com/docs/githooks

您可以使用 --no-verify 开关绕过此问题,请参阅http://git-scm.com/docs/githooks

回答by Phil

As hobbs wrote, the sample pre-commit hook may be doing a check for trailing whitespace. To disable it, look in .git/hooksand make sure the files there are not executable.

正如 hobbs 所写,示例 pre-commit 钩子可能会检查尾随空格。要禁用它,请查看.git/hooks并确保那里的文件不可执行。

One possible way that the hooks may have gotten enabled is that the executable bit may have gotten set if you were moving your repo around on a FAT-formatted flash drive.

挂钩可能已启用的一种可能方式是,如果您在 FAT 格式的闪存驱动器上移动存储库,则可执行位可能已设置。

回答by YuriAlbuquerque

It's not a matter of VCS trying to tell you what good programming style is. It is a matter that trailing whitespace are a bad thing to them, because trailing whitespace can be seen as a useless modification by the VCS.

这不是 VCS 试图告诉您什么是好的编程风格的问题。尾随空格对他们来说是一件坏事,因为尾随空格可以被 VCS 视为无用的修改。

How so?

为何如此?

Imagine you have a line like this:

想象一下你有这样一行:

puts "Hello World"^M^M^M

Where each "^M" is a trailing whitespace (put there to simplify).

其中每个“^M”是一个尾随空格(放在那里是为了简化)。

Another developer accidentally changes this like to this:

另一个开发人员不小心将其更改为:

puts "Hello World"^M^M

The VCS will see this as a change. A useless one that does not interferes with the code at all, but a change. This kind of change can even be pointed by the VCS as a conflict (which is something that should be avoided whenever possible). Besides, it pollutes your history without need.

VCS 会将此视为更改。一个根本不干扰代码的没用的,而是一个改变。这种变化甚至可以被 VCS 指出为冲突(这是应该尽可能避免的事情)。此外,它会在不需要的情况下污染您的历史记录。