Skip to content

Instantly share code, notes, and snippets.

@brlodi
Last active August 22, 2023 20:13
Show Gist options
  • Save brlodi/321d4ebe953197d257f6f22c2f5d9c9b to your computer and use it in GitHub Desktop.
Save brlodi/321d4ebe953197d257f6f22c2f5d9c9b to your computer and use it in GitHub Desktop.
Rich git diffs for images on the command line

If you use a terminal emulator that supports imgcat (most commonly and famously iTerm 2 implements it), drop this script into your path and add the attached snippet to your .gitignore to generate visual diffs in your Git commandline.

Derived from https://til.hashrocket.com/posts/n44hnf0z99-show-better-image-diff-on-git

Prerequisites

  • Image-capable terminal emulator
  • imgcat implementation for your emulator in your path
  • GraphicksMagick available in your path (an ImageMagick clone, minus the security flaws)
[diff "image"]
textconv = imgcat
command = imgdiff
#!/bin/bash
echo "$1"
if [[ -f "$1" ]] && [[ -f "$2" ]]; then
gm compare -highlight-style apply -highlight-color magenta -file png:- "$2" "$1" | gm montage -background transparent -geometry +4 "$2" - "$1" png:- | imgcat
else
if [[ -f "$1" ]]; then
echo "+ Image added"
imgcat "$1"
else
echo "- Image removed"
imgcat "$2"
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment