Skip to content

Git diff side by side

Casiva Agustin edited this page Apr 11, 2017 · 5 revisions

How to get a side by side diff with git diff or git difftool in the terminal.

Alternative 1

Use icdiff, as instructed on https://blog.scottnonnenberg.com/better-git-configuration/

  1. install icdiff (from https://github.com/jeffkaufman/icdiff but ubuntu also has a package)
  2. add to your ~/.gitconfig:
    [diff]
      tool = icdiff
    [difftool]
      prompt = false
    [difftool "icdiff"]
      cmd = /usr/local/bin/icdiff --line-numbers $LOCAL $REMOTE
  1. use git difftool ... to use instead of git diff, e.g. git difftool master to compare current branch and master.

Example output:

Example screen shot

Alternative 2

Requirements:

  • sdiff (from diffutils)
  • colordiff

Place the following in your PATH (eg ~/bin/diffy):

#!/bin/sh
echo
echo Comparing: "$1 between $3 and $6"
echo
if stty >/dev/null 2>&1; then
  pager=${PAGER:-less -r}
else
  pager=cat
fi
C=$(stty size | cut -d' ' -f2)
D=$(expr $C / 2)
sdiff -W -w $C "$2" "$5" | colordiff | grep -E "^.{$D} *[|<>] *" -A 10 -B 10 -n | $pager

Then set the environment variable GIT_EXTERNAL_DIFF to point to it.

Example output: Example screen shot