43

git diff does not support a case-insensitive comparison of files. Google shows a very few people asking for that feature, and that too only in combination with some other git diff switch, like with -G or with --color-words.

I don't care for other switches, as long as git diff can show me the case-insensitive diff.

Since I didn't see any specific question towards that, and since I found a solution after an hour researching this problem, I am adding this question and the answer.

Gurjeet Singh
  • 2,635
  • 2
  • 27
  • 22
  • `git diff` also does not support case-insensitive comparison of **file names**. Do you have a solution for that? – Dan Bechard Jun 06 '16 at 18:07
  • Possible duplicate of [How can I do case insensitive git diffing while also doing \`git diff --color\`?](https://stackoverflow.com/questions/4998474/how-can-i-do-case-insensitive-git-diffing-while-also-doing-git-diff-color) – Evan Carroll Oct 24 '18 at 07:49
  • @DanBechard I'd just rely on Git deciding that those are "renames." It adds very little noise to the diff output. – Michael Jun 11 '19 at 14:54

4 Answers4

39

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

UPDATE (2021/11/26): Here's a consolidated configuration I use, that allows me to use git idiff command that behaves almost identical to how git diff behaves.

git config --global difftool.idiff.cmd 'diff --unified=3 --color=always --ignore-case $LOCAL $REMOTE | less --raw-control-chars'
git config --global difftool.prompt 0
git config --global alias.idiff 'difftool --tool idiff'
Gurjeet Singh
  • 2,635
  • 2
  • 27
  • 22
  • 1
    but git-gui uses 'git diff' and not 'git difftool', so git-gui still shows all case sensitive differences .. – Klaus Mar 11 '16 at 10:42
  • 2
    You can install `colordiff` first (e.g. `sudo apt-get install colordiff`) and then replace `diff -i $LOCAL $REMOTE` with `diff -i -u $LOCAL $REMOTE | colordiff` for colored unified diffs, i.e. normal Git style. – caw Dec 08 '16 at 05:52
  • 1
    i added some more options to the `diff` command `git config --global difftool.idiff.cmd 'diff -i --unified --ignore-space-change --ignore-blank-lines --ignore-all-space $LOCAL $REMOTE'`. see `man diff` for more info on what the additional options are. – Trevor Boyd Smith Mar 12 '18 at 17:18
  • 1
    i was trying to redirect the output to a file... but it kept hanging until i did `git config --global difftool.prompt 0`. – Trevor Boyd Smith Mar 12 '18 at 17:19
  • can add `--color=always` as an option to `diff` too instead of piping to `colordiff` (and then use with `less -r` for pagination) – gawkface Nov 27 '21 at 01:38
4

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.

Michael
  • 8,362
  • 6
  • 61
  • 88
0

A bit fixed - no need to have useless colors in patch:

git config --global difftool.idiff.cmd 'diff --unified=3 --color=never --ignore-case $LOCAL $REMOTE | less --raw-control-chars'
git config --global difftool.prompt 0
git config --global alias.idiff 'difftool --tool idiff'

And then it should be possible to generate nice HTML from by for example

<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8" />
    <!-- Make sure to load the highlight.js CSS file before the Diff2Html CSS file -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css" />
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css"
    />
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
  </head>
  <script>
    function show(diffString) {
      var targetElement = document.getElementById('myDiffElement');
      var configuration = {
        drawFileList: true,
        fileListToggle: false,
        fileListStartVisible: false,
        fileContentToggle: false,
        matching: 'lines',
        outputFormat: 'side-by-side',
        synchronisedScroll: true,
        highlight: true,
        renderNothingWhenEmpty: false,
      };
      var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
      diff2htmlUi.draw();
      diff2htmlUi.highlightCode();
    }
  </script>
  <body>
    <div id="myDiffElement"></div>
    <textarea onchange="show(this.value)" rows=30 cols=160></textarea>
  </body>
</html>
Jan
  • 2,178
  • 3
  • 14
  • 26
  • Or check this https://stackoverflow.com/questions/70640721/visual-diff-patch-editor question - simple page transforming -/+ rows by 2 functions (regExp for example) to be similar (replace whitespaces, etc.) and filtering only results different after this transformation on both sides... – Jan Jan 20 '22 at 18:47
  • https://meldmerge.org/ or https://github.com/timbrel/GitSavvy looks interesting... – Jan Jan 24 '22 at 11:12
0

To compare two Git commits regardless of case, I create archive files and compare the file contents with an external tool - in this case WinMerge (Portable):

git archive 991f70fe --format=zip > Archive/arcL.zip
git archive cffd466a --format=zip > Archive/arcR.zip
WinMergePortable.exe /ignorecase /ignoreeol /m Full Archive/arcL.zip Archive/arcR.zip

or

git archive master --format=zip > Archive/arcL.zip
git archive master --format=zip --remote=ssh://user@192.168.1.2/volume1/git/Tools20 > Archive/arcR.zip
WinMergePortable.exe /ignorecase /ignoreeol /m Full Archive/arcL.zip Archive/arcR.zip 

The last command opens the GUI that shows the differences.

Falo
  • 331
  • 3
  • 5