8

I have recursively replaced many single word patterns in my code base. Before committing I need to check each for false replacements. It would help me a lot to have git add -p use the format of what --word-diff uses with git diff, where only the changed words are marked and not the entire line.

Someone has asked the same here, but maybe it was implemented since? https://groups.google.com/forum/#!topic/git-users/7uO2OUJGvP0

djangonaut
  • 7,233
  • 5
  • 37
  • 52
  • 1
    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) – Olivier Le Floch Sep 08 '18 at 00:51

2 Answers2

5

Since Git 2.9, you can use the property interactive.diffFilter (as I mentioned below this answer)

But, any git -c interactive.diffFilter="git diff --color-words" add -p would generate, since Git 2.17, an error message

fatal: mismatched output from interactive.diffFilter

(as seen in this question)

So using a script for the interactive.diffFilter setting remains a safer way.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The `--color-words` example doesn't seem to work properly. It shows each chunk for staging multiple times and even when certain chunks have been rejected during interactivity, they end up being staged. – dionyziz Nov 10 '17 at 12:35
  • @dionyziz What version of git are you using? – VonC Nov 10 '17 at 12:36
  • I am using git version 2.13.3. – dionyziz Nov 10 '17 at 21:47
  • @dionyziz Does it persists with the latest 2.15? – VonC Nov 10 '17 at 21:50
  • Doesn't work at all with git 2.19. `fatal: mismatched output from interactive.diffFilter` `hint: Your filter must maintain a one-to-one correspondence between its input and output lines`. – leftaroundabout Sep 26 '19 at 09:11
  • @leftaroundabout True, that is now enforced since Git 2.17 (https://stackoverflow.com/q/53213708/6309). I have amended the answer. – VonC Sep 26 '19 at 09:28
2

Adding diff-highlight to the interactive.diffFilter config key is the easiest option (since Git 2.9). The following comand does the trick on Debian/Ubuntu:

git config interactive.diffFilter "perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight"

Things like git -c interactive.diffFilter="git diff --color-words" add -p or git config interactive.diffFilter "git diff --color-words" don't work properly: add -p always keeps suggesting the first modified file.

sgtpep
  • 819
  • 1
  • 7
  • 3