How to use --color-words with git add --patch?

前端 未结 7 1814
梦谈多话
梦谈多话 2020-12-07 17:27

When diffing files, I prefer to use git diff --color-words. Is there a way to make this the default format for diffs when using git add --patch or

7条回答
  •  旧巷少年郎
    2020-12-07 18:00

    I recently solved this issue, but it requires modifying a Perl script in git. That's easy and requires no special skill, however.

    This solution requires that your git configuration use colorization for screen output, because that's the only circumstance under which git will show a word-based diff.

    1. Copy git-add--interactive from your installation to somewhere in your PATH environment variable and rename it git-add--interactive-words.
    2. Edit a line about half way down to change*
    @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --), $path);
    

    to

    @colored = run_cmd_pipe("git", @diff_cmd, qw(--color --color-words --), $path);
    
    1. You can now run git add-interactive--words to do the equivalent of git add --interactive with colorized word-based diff.
    2. However, combining git add --patch with that is awkward because you need to pass the new script the right parameters. Fortunately, you can create an alias to the magic words in your .gitconfig:
    [alias]
    iaddpw = add--interactive-words --patch=stage --
    

    which means git iaddpw runs the equivalent of git add --interactive --patch with colorized word-based diff.


    *- For Git 2.18, this command is:

    my @display_cmd = ("git", @diff_cmd, qw(--color --), $path);
    

提交回复
热议问题