How to perform case insensitive diff in Git

♀尐吖头ヾ 提交于 2019-11-29 03:02:14

The solution is to use git difftool. With the following config command, I can add a custom diff tool called idiff for Git to use:

git config --global difftool.idiff.cmd 'diff -i $LOCAL $REMOTE'

With this customization, I can see the case-insensitive comparison like so:

git difftool --tool idiff <other diff options> <Git references or files>

Eg.

git difftool -t idiff HEAD~1 -- my_schema.sql

Since git difftool prompts (for yes/no) every time before invoking the the tool, either use -y switch for difftool or add this config option to avoid the prompt:

git config --global difftool.prompt 0

Brandon McCaig

To expand on Gurjeet Singh's answer, and caw's colordiff comment, in Windows I did the following to get it all tied together:

  1. If needed, install Strawberry Perl distribution for Windows. Any Windows Perl distribution should work, but Strawberry Perl is free software/open source and comes with batteries included. Heed caution with the Web site result because there's a NSFW site with a similar domain IIRC. Use Google instead of guessing.

  2. Install MinGW/MSYS. Git for Windows already comes with a build of MSYS so you might be able to just use its make, but your mileage may vary.

  3. Download and install the colordiff Perl script. I edited the Makefile to change the install location to ~/bin and ~/etc (where ~ is %USERPROFILE%) because ~/bin is already in my PATH. Adjust as needed.

  4. (cmd.exe) Edit your registry environment variables (search start menu for environment variables) and add .PL to PATHEXT (and whatever bin/ you used for PATH if necessary).

  5. (cmd.exe) Create a bash script (e.g., ~/bin/colordiffless.bash) that passes any arguments on to colordiff.pl (colordiff accepts diff options and passes them on automatically) and pipes through less. The color codes output by colordiff are ANSI, which cmd.exe will not understand, but less does. You also restore Git's pager behavior this way (configure LESS environment variable if necessary).

    #!/bin/bash
    colordiff.pl "$@" | less
    
  6. Setup the alias as Gurjeet did, except instead of invoking diff directly, invoke your bash script. The color codes output are ANSI so you'll need something to convert them. I happen to know that MSYS less will do just that, and you also preserve the pager behavior of Git!

    git config --global difftool.cldiff.cmd "colordiffless.bash -ui $LOCAL $REMOTE"
    

    (from cmd.exe, so the double-quotes are literal and the $LOCAL and $REMOTE are literal text too)

  7. Finally, alias the difftool command so you can type a single, custom command instead of the difftool command:

    git config --global alias.cldiff "difftool -y -t cldiff"
    

Edit

I was mistaken about the pager behavior coming back. difftool invokes the command for each file so instead of getting a single pager output with every diff, you will get a pager for each file. To solve that, you will likely want to wrap difftool -y in a script and pipe its entire output to less instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!