Skip to content

Instantly share code, notes, and snippets.

@stevekm
Last active September 9, 2021 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevekm/a1bd98b1c75219c4ea15b954b8e86eab to your computer and use it in GitHub Desktop.
Save stevekm/a1bd98b1c75219c4ea15b954b8e86eab to your computer and use it in GitHub Desktop.
git diff explanation & gdiff shortcut
Sample1
Sample2
Sample3
Sample4
Sample5
Sample1
Sample4
Sample6
Sample7
Sample8
#!/bin/bash
# use git to diff two files or dirs at the per-character level
gdiff () {
git diff --no-index --word-diff=color --word-diff-regex=. "${1}" "${2}"
}

A visual example for interpretting the output of git diff, along with a helper alias gdiff to make it easier to git diff two files or directories.

Example input files:

$ cat example1.txt
Sample1
Sample2
Sample3
Sample4
Sample5

$ cat example2.txt
Sample1
Sample4
Sample6
Sample7
Sample8

Diff'ing example1 vs example 2

$ gdiff example1.txt example2.txt

Screen Shot 2021-09-09 at 10 51 27 AM

  • The lines & characters in red are present in example1 but missing in example1

  • The lines & characters in green are present in example2 but missing in example1

Diff'ing example2 vs. example1

$ gdiff example2.txt example1.txt

Screen Shot 2021-09-09 at 10 51 35 AM

  • The lines & characters in red are present in example2 but missing in example1

  • The lines & characters in green are present in example1 but missing in example2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment