28

git add --patch provides a great interface for reviewing unstaged changes and then staging only the ones that are wanted in the next commit.

Great, except for one thing: there is no obvious way to choose which diff view to use.

In particular, I would like to be able to configure git add --patch to present diffs to me the same way that git diff --word-diff does.

How can I achieve that?

(N.B. neither --word-diff nor --word-diff --color is exactly the same as --color-words, and so this question (and its answer) are different to this question and its answers. However, that question's answers are much more comprehensive than this one's, so that is probably the place to go for more information about how to do achieve things like this.)

3

1 Answer 1

7

This is sort of possible, as follows:

git -c interactive.diffFilter="git diff --word-diff --color" add --patch

However, what Git shows you, and what will actually be staged as a result of your interactive commands, will not necessarily match.

Sign up to request clarification or add additional context in comments.

3 Comments

This no longer works in git 2.17+ and fails with fatal: mismatched output from interactive.diffFilter hint: Your filter must maintain a one-to-one correspondence hint: between its input and output lines.
@OlivierLeFloch, I understand this to be a stop-gap measure to avoid the silent introduction of errors. In the long run, of course, a means to achieve an error-free git add --patch --word-diff would be very desirable.
I actually worked on this some more and detailed a solution here: stackoverflow.com/a/52231087/163677 using interactive.diffFilter and diff-highlight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.