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

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

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

    3) 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:

1)

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

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

2)

 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?

sushain97
  • 2,752
  • 1
  • 25
  • 36
user1742529
  • 260
  • 4
  • 16

2 Answers2

0

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:

/home/user/truepower

New good repository:

/home/user/onepower

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 \
trailing-space,space-before-tab,indent-with-non-tab

# 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: http://git-scm.com/book/en/Customizing-Git-Git-Configuration#Formatting-and-Whitespace

Community
  • 1
  • 1
user1742529
  • 260
  • 4
  • 16
  • 4
    In addition to being insanely complicated this doesn't seem to answer your original question at all – Andrew C Sep 28 '14 at 20:29
  • @AndrewC It answers my question. It totally solves my problem. This solution is very simple and easy. You even can automate this process if you want. Nobody could get me this answer. – user1742529 Sep 28 '14 at 23:23
  • 1
    You asked how to restrict the `git add` operation, and your "solution" involved multiple repositories and steps that occur not only after `git add` but `git commit`. It looks like it could be accomplished much simpler with a trivial filter-branch operation, or even a .gitattributes clean filter. – Andrew C Sep 28 '14 at 23:27
  • @AndrewC I asked how to restrict `git add` operation to ignore some whitespaces changes, which we can ignoring safely. The answer is to set `git config core.autocrlf` and `git config core.whitespace`. It is in my answer as you can see. But we also need to fix broken (with commits of whitespace changes) repository first. It is also in my answer. You can't save repository commit history if modify some of commits. So it's no problem to create new repository. – user1742529 Sep 29 '14 at 02:11
  • @AndrewC Using filter-branch is not trivial. My solution in answer is trivial. filter-branch will not fix `core.autocrlf` and `core.whitespace`, so your advice is incorrect, because in your variant we will have problems with `git add` again in the future. – user1742529 Sep 29 '14 at 02:12
0

Add the following to your .gitconfig to:

  1. Add only new files

    adduntracked=!git add $(git ls-files -o --exclude-standard)

  2. Add only non-whitespace changes:

    addnows = !git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"

  3. Both together:

    addnewnows=!git add $(git ls-files -o --exclude-standard) && git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"

Community
  • 1
  • 1
Tom Hale
  • 40,825
  • 36
  • 187
  • 242