user1742529 user1742529 - 1 year ago 172
Git Question

How to git-add only non-whitespace changes and new files?

  1. We can do diff with some types of ignoring whitespaces:

    git diff --ignore-space-at-eol
    # Ignore changes in whitespace at EOL.

    git diff --ignore-space-change
    git diff -b
    # Ignore changes in amount of whitespace.

    git diff --ignore-all-space
    git diff -w
    # Full whitespace ignoring

  2. We can do

    git apply --ignore-whitespace
    git apply --ignore-space-change
    # Ignore whitesapces when applying patch

But how to exclude files with whitespaces changes from
git add *

These solutions does not work for me:


git diff -w --no-color | git apply --cached --ignore-whitespace

- It sometimes writes errors and does not add new files to tracking.


git add `git diff -w --ignore-submodules |grep "^[+][+][+]" |cut -c7-`

- It writes errors and do nothing (maybe because I have binary files, not only text files)

P.S.: And maybe there is way to replace files (with whitespaces differences at end or line and whitespaces before EOF differences) with files from last commit?

Answer Source

The only one real solution of this problem is.

Solution is to recreate git repository with special setting and then copy to this repository from original commits from one checkout state to other.

Source bad repository:


New good repository:


cd /home/user
rm -rf ./onepower
mkdir ./onepower
cd ./onepower
git init

# set style of lineendings to be autoconverted to Linux/Unix LF
#git config core.autocrlf true # uncomment this if you prefer Windows CRLF style
git config core.autocrlf input # comment this if you prefer Windows CRLF style

# set trailing whitespace (and other similar) to ignore
git config core.whitespace \

# you can use git config --global ... if you want global settings changing.

cd ../truepower
git log

commit cccc

commit bbbb

commit aaaa

cd ../truepower
git checkout aaaa
cd ../onepower
rm -rf ./*
cp -a ../truepower/* ./
git add -A
git commit -m "fixed aaaa"

cd ../truepower
git checkout bbbb
cd ../onepower
rm -rf ./*
cp -a ../truepower/* ./
git add -A
git commit -m "fixed bbbb"

cd ../truepower
git checkout cccc
cd ../onepower
rm -rf ./*
cp -a ../truepower/* ./
git add -A
git commit -m "fixed cccc"

Now you can remove old bad ../truepower and use new ../onepower git repository.

Btw, after this you will not have problems with this repository with whitespace changes in the end of file, in the end of string, and partially in the begin of string. But of course whitespace changes in middle of string will be interpreted as changes.

Solution found with help of:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download