How can I do git add with patch mode but ignoring whitespace changes.
The use case is for when you\'ve reformatted a file and also made changes to it. I want to commit t
Here's an adaptation from a related question.
git diff -w --no-color | git apply --cached --ignore-whitespace
It has the benefit that you don't need to use stash
, temporary files, or perform a reset --hard
on your working folders.
Addendum
The solution above only stages changes except whitespace-only edits. This did not address patch, though using --patch
to stage isn't straight forward in this situation.
Patch Option 1: Edit the diff in a text editor
There are many ways to implement this using a text editor. Vim is especially suited to this.
In the root directory of your repository, start Vim.
In normal mode, load the diff into an empty buffer with...
:r !git diff -w --no-color
:set ft=diff # if you want syntax highlighting
Edit the diff and remove the parts you don't want to stage.
To stage the contents of the vim buffer, run the vim ex command...
:w !git apply --cached --ignore-whitespace
If you're a Vim afficionado, you could use visual mode to stage, too!
:<',>'w !git apply --cached --ignore-whitespace
You can commit the staged changes with the ex command...
:!git commit -m "message"
# or
:!git commit
Clear the buffer, read the unstaged changes, and repeat
:bd! | set ft=diff | r !git diff -w --no-color
Eventually, you'll be left with only whitespace changes to commit.
If you don't use Vim, you could also dump git diff
into a file, edit the file, save, then feed the file into git apply
. Commit and repeat until done. It's a bit tedious, but functional.
Patch Option 2: Patch reset
It's backwards from git add --patch
, but once you've staged non-whitespace changes with...
git diff -w --no-color | git apply --cached --ignore-whitespace
...you can unstage chunks in patch mode with...
git reset --patch .
Keep in mind you're removing the changes that you want to keep staged. Repeat and commit as necessary until you only have whitespace changes left.