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

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.

  • git commit command with core.editor equals atom
  • Gitweb not displaying some repos
  • Automatically versioning Android project from git describe with Android Studio/Gradle
  • How can I place my Meteor apps version number in the UI?
  • Git submodule is in “detached head” state after cloning and submodule update
  • ZSH prompt not showing git unstaged files
  • 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.

  • Cannot trigger post-commit git hook on git submodule
  • What happens to OpenPGP-signed git commits after key expiration?
  • Case typo on path in msysgit
  • How to get next commit to apply when using git rebase?
  • Ignore fsck / zero-padded file mode errors in “git clone”
  • Papyrus restore diagram
  • 2 Solutions collect form web for “Any way in git, to add all files with only whitespace changes to staging?”

    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
    

    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)
    
    Git Baby is a git and github fan, let's start git clone.