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.
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.
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) 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)