有没有办法用 git diff 显示*仅*空白差异? [英] Is there a way to show *only* whitespace differences with git diff?
问题描述
我想在他们自己的提交中提交我所有的空白更正,以保持其他所有内容不受空白更改的影响.
I'd like to commit all my whitespace corrections in their own commit, to keep everything else pure from whitespace changes.
很容易过滤出空白差异与 git diff
使用这样的东西
It's easy to filter out whitespace differences with git diff
using something like this
git diff --ignore-all-space --ignore-space-change --ignore-space-at-eol --ignore-blank-lines
但是我如何获得仅空白差异的列表?
but how do I get a listing of only whitespace differences?
(获取仅有空格差异的文件列表也很有用,因此我可以添加
所有这些文件而无需使用git add -p
来找出空格差异.但我想这是次要的.)
(It would also be useful to get a list of files that only have whitespace differences, so I can just add
them all without going through them with git add -p
to pick out the whitespace differences. But I suppose that's secondary.)
推荐答案
这里有一种方法可以做到:
Here's one way you could do it:
从一个干净的工作目录开始(例如
git reset --hard
)
使用 Whitespace Total Fixer 清除所有空白错误.有修复或忽略各种问题的选项,但默认值可能没问题:
Use Whitespace Total Fixer to clean up all the whitespace errors. There are options to fix or ignore various issues but the default may be fine:
find . -name "*.whatever_extension_you_need" -exec wtf -v -i {} \;
git diff
现在将仅显示空格更改
git diff
will now show only the whitespace changes
暂存并提交仅包含空格的更改.
Stage and commit the whitespace-only changes.
(FWIW,我写了 wtf
;如果这看起来像是无耻的自我推销,我很抱歉,但我确实是专门为这种情况编写的,其中存储库反复被空格问题污染这会破坏你的提交.)
(FWIW, I wrote wtf
; I'm sorry if it seems like shameless self-promotion, but I did write it specifically for situations like this one where a repository repeatedly becomes contaminated with whitespace issues that gum up your commits.)
您还可以使用 wtf
来简单地检查空白错误,而不是就地修复它们;这不会影响您的文件,但它会向 stderr 打印一条(希望有用)关于它发现的问题的消息.
You can also use wtf
to simply check for whitespace errors rather than in-place fixing of them; this won't affect your files but it will print a (hopefully helpful) message to stderr about the issues it has found.
> find . -name "*.whatever_extension_you_need" -exec wtf -v {} \; > /dev/null
nightmare.txt LINE 8: WARNING: spaces followed by tabs in whitespace at beginning of line
nightmare.txt:
CHOPPED 1 lines with trailing space
CHOPPED 0 blank lines at EOF
ADDED newline at EOF
CHANGED 1 line endings which didn't match crlf from first line
WARNED ABOUT 1 lines with tabs/spaces mix
这篇关于有没有办法用 git diff 显示*仅*空白差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!