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
git - trim 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 gitconfig
file:
要警告未来的空格错误(尾随空格和tabs 之前的空格),并修复补丁中的空格错误,请将以下代码添加到您的gitconfig
文件中:
[core]
whitespace = trailing-space,space-before-tab
[apply]
whitespace = fix
回答by mipadi
core.whitespace
instructs git to flag certain whitespace problems:
core.whitespace
指示 git 标记某些空白问题:
trailing-space
warns about whitespace at the end of a line or at the end of a filespace-before-tab
warns when there is a space before a tab used for indentation
trailing-space
警告行尾或文件末尾的空格space-before-tab
当用于缩进的制表符前有空格时发出警告
apply.whitespace
is 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 .gitconfig
file at the root of your user's home directory (typically /home/user/.gitconfig
on Linux, /Users/user/.gitconfig
on 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 rebase
to 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:]]*$//' {} \;