19

我不小心在我的初始提交中添加了一些空格 - 它在git diff --color. 摆脱现有空白的最佳方法是什么,如何避免再次发生这种情况?

我不一定要寻找内置的git命令。任何在 Ubuntu 上免费提供的外部程序也将受到欢迎。

4

4 回答 4

28

要修剪当前目录中所有文件的尾随空格,请使用:

sed -i 's/[[:space:]]*$//' *

要警告未来的空格错误(尾随空格制表符之前的空格),并修复补丁中的空格错误,请将以下代码添加到您的gitconfig文件中:

[core]
    whitespace = trailing-space,space-before-tab
[apply]
    whitespace = fix
于 2010-07-30T18:48:29.477 回答
14

core.whitespace指示 git 标记某些空白问题:

  • trailing-space警告行尾或文件尾的空格
  • space-before-tab用于缩进的制表符前有空格时发出警告

apply.whitespace在应用补丁时使用。它检查空格错误(上面列出的那些,在 中core.whitespace)并在尝试修复它们(即删除它们)后应用补丁。

这些选项进入~/.gitconfig- 即.gitconfig用户主目录根目录下的文件(通常/home/user/.gitconfig在 Linux 上,/Users/user/.gitconfig在 Mac OS X 上,我不知道在 Windows 上的位置,但我想在某个位置C:\Documents and Settings\user)。

于 2010-07-30T17:36:13.863 回答
2

请参阅此线程git remove trailing whitespace in new files before commit on using git rebaseto strip whitespace from your file.

于 2014-10-10T12:45:05.627 回答
1

为了从所有子目录递归地修剪所有文件中的空格,可以使用它。

find ./* -type f -exec sed -i 's/[[:space:]]*$//' {} \;
于 2018-02-22T16:38:52.280 回答