How can I make git diff always use the --word-diff option?
Asked Answered
R

1

10

What do I have to write in the .gitconfig file in order for git diff to behave as it was git diff --word-diff?

For now, searching for \bword in man git-config I've found diff.wordRegex, and given that

The name is actually the section and the key separated by a dot

from the same man page, I've verified that

[diff]
  wordRegex = some-regex

affects the diff when I run git diff --word-diff. For instance, wordRegex = "." causes a character-wise diff.

But how do I tell git to always use --word-diff (or --color-words, fwiw)?


I know aliases exist, and I use some, but I was curious if there's an option.


Of the two answers at a similar question,

  • the first one suggests to use an alias, which is not what I'm looking for, as written above;
  • the second one is about GUI, which I'm not interested into either.
Rhotacism answered 11/3, 2022 at 15:51 Comment(4)
Would an alias work? git config --global alias.diff "diff --word-diff". FWIW, I use the alias name wd for this.Reproval
@wjandrea: You can't alias an actual Git command (because Git would then get stuck in an endless expansion loop, which is a silly reason, but that's the reason). Using git wd is the way to go.Japanese
Does this answer your question? Is it possible to make --word-diff default in git diff, git gui, gitkEasley
@Joe, no it doesn't, as per my edit. I've also added a tag to clarify even better.Rhotacism
B
0

You can set it per repo by editing the .gitconfig file like this article recommends, for instance by installing diffr and makig this change:

# .gitconfig
[pager]
  # Use `diffr` (make sure it is installed).
  log = diffr | less
  show = diffr | less
  diff = diffr | less

And then git bash will highlight word level diffs automatically.

Or you could just ignore git diff entirely, and use simplewdiff on MacOS to do simplewdiff file1 file2 and see the word level diffs.

Bidarka answered 6/4, 2024 at 3:9 Comment(3)
Would you please include in the text what the solution to my question is?Rhotacism
@Rhotacism done! Let me know if those solutions solve your issue -- they don't address the problem directly, but seem to get you what you're looking for via other ways.Bidarka
+1, but this is not what I asked for. This solution offloads the decision of how to do the diff to another program, whereas I asked how to use --word-diff by default, which means that one could still set wordRegex to decide how to do the diff.Rhotacism

© 2022 - 2025 — McMap. All rights reserved.