Resetting and readding (recommended)
This is the simplest method.
There's no one-line-straight-forward way if you don't want to reset and readd in your working directory. You should do exactly 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 terminal formatting and colors
git apply --cached --ignore-whitespace
applies the diff ignoring whitepace, and indexes it
git checkout -- .
removes the unindexed “whitespace” changes
git reset
resets the index to just the non-whitespace changes
git add -p
adds the non-whitespace changes in patch mode
(source here)
Without resetting
This partially answers your problem, without stashing or resetting.
You can "diff and apply" only non-whitespace changes, in a way like this:
git diff -w --no-color | git apply --cached --ignore-whitespace
This is another way, but please notice that patching here is a bit of a mess, you need to buffer and manually change diff removing whitespaces.
If you use VIM, here's quick step-by-step commands you can use to bufferize, quickly find, remove and finally apply clean 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 remove what you don't want to stage, then
:w !git apply --cached --ignore-whitespace
to apply current fixed diff
and eventually commit this diff with :!git commit -m "your fixed commit"
Here's the first iteration. You need to clear the buffer, read the unstaged changes and repeat:
:bd! | set ft=diff | r !git diff -w --no-color
keep going and, eventually, you'll be left with only whitespace changes to commit.
If you don't want to use VIM:
- dump git diff into a file
- edit the file with your favourite IDE/text editor
- feed the edited file into git apply.
- commit
- repeat until done.
It may not be the fastest way, but it's functional and does the trick if you don't like the first method.
(source and similar question here)
git add
– Outdate