Git add -patch ... but ignore whitespace changes
I can't find a good way to use it git add -p
, but tell git to ignore all whitespace changes. I don't want to reset my local changes.
Situation: I had all my changes locally and they grouped them into separate commits. Then I experimented with minifig and overwritten all my css files with their minified version. I tried "un-minifying" everything, but it still messed up the git diff - because there were so many whitespace changes - and I can't get my repo back to where I can see the actual changes.
Thanks for your help!
source to share
Reset and read (recommended)
This is the easiest method. There is no one-line direct path unless you want to reset and readd in your working directory. You have to do just that, here's a good way explained step by step:
git diff -w --no-color | git apply --cached --ignore-whitespace && git checkout -- . && git reset && git add -p
git diff -w --no-color
creates a diff without formatting or colors
git apply --cached --ignore-whitespace
applies whitespace ignoring whitespace and indexes it
git checkout -- .
removes non-indexed "spaces" changes
git reset
resets the index with only changes without spaces
git add -p
adds changes without spaces in patch mode
( source here )
No reset
This will partially answer your problem without erasing or resetting. You can only "distinguish and apply" changes without spaces, for example:
git diff -w --no-color | git apply --cached --ignore-whitespace
This is another way, but please note that the fix here is a bit of a mess, you need to buffer and manually modify the diff to remove the spaces. If you are using VIM, here are quick step-by-step commands you can use to buffer, quickly find, delete, and finally apply pure diffs:
:r !git diff -w --no-color
this creates a new buffer with your diff
:set ft=diff
(optional) use this if you want syntax highlighting
now you need to manually delete what you don't want and then
:w !git apply --cached --ignore-whitespace
apply current fixed diff
and ultimately pass this diff with :!git commit -m "your fixed commit"
Here's the first iteration. You need to clear the buffer, read the unsettled changes, and repeat:
:bd! | set ft=diff | r !git diff -w --no-color
keep going and in the end you are left with only whitespace changes to commit.
If you don't want to use VIM:
- dump git diff to file
- edit the file with your favorite IDE / text editor
- submit the edited file to git.
- commit
- repeat to completion.
This may not be the fastest way, but it works and does the trick if you don't like the first method.
(source and similar question here )
source to share