18

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.)

  • Possible duplicate of [How to use --color-words with git add --patch?](https://stackoverflow.com/questions/10873882/how-to-use-color-words-with-git-add-patch) – phd Mar 01 '18 at 21:41
  • @phd, thanks. It is similar, but not the same. I have now clarified this. –  Mar 01 '18 at 21:56
  • Duplicate of: https://stackoverflow.com/questions/49278577/how-to-improve-gits-diff-highlighting/60970801#60970801 . Please give a look at the answers there. – Zorglub29 Apr 01 '20 at 12:40

1 Answers1

5

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.

  • 5
    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. ``` – Olivier Le Floch Sep 08 '18 at 00:46
  • @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. –  Sep 08 '18 at 14:30
  • 2
    I actually worked on this some more and detailed a solution here: https://stackoverflow.com/a/52231087/163677 using `interactive.diffFilter` and `diff-highlight`. – Olivier Le Floch Sep 10 '18 at 21:43