git - 修剪空白

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

git - trim whitespace

gitwhitespacecode-cleanupremoving-whitespace

提问by Zaz

I've accidentally put some whitespace in my initial commit- it shows up red in git diff --color. What's the best way to get rid of the existing whitespace and how can I avoid this happening again?

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

I am not necessarily looking for a built-in gitcommand. Any external program available for free on Ubuntu would also be welcome.

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

回答by Zaz

To trim trailing whitespace on all files in the current directory, use:

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

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

To warn about future whitespace errors (both trailing spacesand spaces before tabs), and to fix whitespace errors in patches, add the following code to your gitconfigfile:

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

[core]
    whitespace = trailing-space,space-before-tab
[apply]
    whitespace = fix

回答by mipadi

core.whitespaceinstructs git to flag certain whitespace problems:

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

  • trailing-spacewarns about whitespace at the end of a line or at the end of a file
  • space-before-tabwarns when there is a space before a tab used for indentation
  • trailing-space警告行尾或文件末尾的空格
  • space-before-tab当用于缩进的制表符前有空格时发出警告

apply.whitespaceis used when applying a patch. It checks for whitespace errors (the ones listed above, in core.whitespace) and applies the patch after attempting to fix them (i.e., remove them).

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

These options go in ~/.gitconfig-- that is, a .gitconfigfile at the root of your user's home directory (typically /home/user/.gitconfigon Linux, /Users/user/.gitconfigon Mac OS X, and I don't know where on Windows but I suppose somewhere in C:\Documents and Settings\user).

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

回答by stevejpurves

See this thread git remove trailing whitespace in new files before commiton using git rebaseto strip whitespace from files that you've already committed.

请参阅此线程git remove trailing whitespace in new files before commit用于git rebase从您已提交的文件中去除空格

回答by rshdzrt

And to trim the white spaces from all files recursively from all sub directories this can be used.

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

find ./* -type f -exec sed -i 's/[[:space:]]*$//' {} \;

find ./* -type f -exec sed -i 's/[[:space:]]*$//' {} \;