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

2

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?

Ley answered 30/7, 2014 at 18:48 Comment(5)
First of all, you should avoid commiting binary files, at all. Second, can you provide us the error message(s) you have sometimes?Embow
Why do you need to make whitespace changes locally that you don't want to commit?Boni
I want to see real changes in my git repository tree, but I have too many whitespace changes.Ley
possible duplicate of Git add only non-whitespace changesBlessed
Even better for me would be add -p to show only non-white space changesBunkum
L
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

Ley answered 28/9, 2014 at 19:57 Comment(5)
In addition to being insanely complicated this doesn't seem to answer your original question at allHodges
@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.Ley
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.Hodges
@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.Ley
@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.Ley
T
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 "#"

Thermoscope answered 14/9, 2016 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.