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.

  • How can I use git to provide a word count graph?
  • Permission denied on git clone of my own private repo on a remote server - even though I added a new public key and all
  • TortoiseGit: “Stash POP Fail!!!” repeats, even after resolving conflict
  • Download a Git repo from BitBucket
  • Is there a graphical way to git stash/unstash individual files?
  • How do you merge a commit that has been reverted?
  • 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

    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.

  • Directory mapping and merging in git
  • How to remove a too large file in a commit when my branch is ahead of master by 5 commits
  • How does git store files?
  • How to connect github with visual studio code?
  • Removing big files from the history/completely removing the history locally and then pushing it to origin
  • Git: can't switch to new remote branch
  • 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 changes whose lines are all blank.

    try something like this:

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

    Submitting my own answer in using Python and the Python sh library (,

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