找到你要的答案

Q:Any way in git, to add all files with only whitespace changes to staging?

Q:在git的任何方式,添加所有文件只有空白的变化么?

An update came in from a library we use that, among other things, cleaned up / standardised up newlines at the end of files. As I consider this change a completely safe change, I'd like to be able to automatically say that any file that has a diff like,

diff --git a/ExampleFile.cs b/ExampleFile.cs
index abcdef..abcd 100755
--- a/ExampleFile.cs
+++ b/ExampleFile.cs
@@ -88,4 +88,4 @@ namespace com.sixminute
         }

     }
-}
\ No newline at end of file
+}

to be put directly into staging, so I only have to deal with / review the "real" files that have changed? Basically, any file that if I run git diff -w ExampleFile.cs on and returns an empty string should be added.

I've looked for a way to do this automatically with git, but I can't see any direct way to be able to do it. My solution was to write a python helper script (git below is just a sh.Command wrapper to the git binary in my shell),

for fname in git.diff('--name-only', '--full-index', 'HEAD').splitlines():
    if '' == git.diff('-w', fname):
        print 'adding', fname
        git.add(fname)

But for something like this, I kinda automatically assumed git would have something built in, but if it is, I can't find the details of it in the docs.

更新来自图书馆的利用,在其他事情上,清理/标准在文件结束符。我认为这种变化的一个完全安全的改变,我想能够自动说任何文件,比较喜欢,

diff --git a/ExampleFile.cs b/ExampleFile.cs
index abcdef..abcd 100755
--- a/ExampleFile.cs
+++ b/ExampleFile.cs
@@ -88,4 +88,4 @@ namespace com.sixminute
         }

     }
-}
\ No newline at end of file
+}

要直接放入分期,所以我只需要处理/审查的“真正”的文件已经改变了吗?基本上,任何文件,如果我运行git diff - W examplefile.cs并返回一个空字符串应加。

我寻找一种方式来做这个自动使用Git,但我看不到任何直接的方式可以做到。我的解决方案是写一个Python辅助脚本(git下面只是一个嘘命令包装Git在我的壳),

for fname in git.diff('--name-only', '--full-index', 'HEAD').splitlines():
    if '' == git.diff('-w', fname):
        print 'adding', fname
        git.add(fname)

但这样的事情,我不由自主地认为Git会有一些内置的,但如果是真的,我不可以在文档中找到它的细节。

answer1: 回答1:

man git-diff says:

--ignore-blank-lines

Ignore changes whose lines are all blank.

try something like this:

for fname in $(git diff --name-only --full-index HEAD)
do
   diff=$(git diff -w --ignore-blank-lines)
   [ -z "$diff" ] && git add $fname
done

男人git diff说:

忽略空行

忽略那些线条都是空白的更改。

尝试这样的东西:

for fname in $(git diff --name-only --full-index HEAD)
do
   diff=$(git diff -w 忽略空行)
   [ -z "$diff" ] && git add $fname
done
answer2: 回答2:

Submitting my own answer in using Python and the Python sh library (https://github.com/amoffat/sh),

import sh

git = sh.git.bake('--no-pager', _cwd='/Developer/repos/git/example')

for fname in git.diff('--name-only', '--full-index', 'HEAD').splitlines():
    diff = git.diff('-w', fname)
    print diff
    if '' == diff:
        print 'adding', fname
        git.add(fname)

在使用Python及其上海图书馆提交自己的答案(http:/ / GitHub。COM / amoffat / SH),

import sh

git = sh.git.bake('--no-pager', _cwd='/Developer/repos/git/example')

for fname in git.diff('--name-only', '--full-index', 'HEAD').splitlines():
    diff = git.diff('-w', fname)
    print diff
    if '' == diff:
        print 'adding', fname
        git.add(fname)
git  whitespace