How to make git-diff create a “context” format diff?

I’ve got a Git repo from which I need to create a patch file in something other than the default git diff format. My use case is I’ve got a clunky old OSF/1 machine on which I need to apply the patch, and the /bin/patch program there doesn’t understand unified diffs.

If I use GIT_EXTERNAL_DIFF=diff, hoping that I can then use GIT_DIFF_OPTS=-c to request a context format diff, then my (modern) diff program complains about extra arguments on its command line:

  • difference between diff outputs using “git add --patch” and “filemerge”
  • Diff a git fork
  • How to perform case insensitive diff in Git
  • Can you stage a file (or at least, a section of a file) DURING a git diff?
  • How to do a git diff on moved/renamed file?
  • How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?
  • diff: extra operand `373e5907b789a1398a91f4ceb4ab14e8a0ed4282'
    diff: Try `diff --help' for more information.
    external diff died, stopping at [filename].

    Setting GIT_EXTERNAL_DIFF=echo shows that Git seems to run the external diff program with:

    $GIT_EXTERNAL_DIFF <file2> <file1> <hash> <mode> <tmpfilename> <hash> <mode>

    This confuses diff which doesn’t want the extra arguments. Is there an easy way to tell git diff to create an old-style “context” format diff?

    (My current plan is to write a one-liner shell script that calls the real diff with just $1 $2, but I’m hoping there is a less awkward way.)

  • Why can a changeless rebase fail?
  • Git LFS skipped files
  • How do I remove an invalid remote I added in Git?
  • How do I fix these spurious git commits?
  • should minified files be committed into source control?
  • Git: Should I ignore the Index or is there a killer application for it?
  • 2 Solutions collect form web for “How to make git-diff create a “context” format diff?”

    Configure a difftool to do what you want:

    $ git config difftool.ctxdiff.cmd 'diff $LOCAL $REMOTE'
    $ git difftool -y -t ctxdiff HEAD~4..HEAD

    There is no need to configure a custom difftool, just use the -x option:

    $ git difftool -y -x "diff -c" | less

    Or configure an alias to make a simple “git cdiff” command output a context-style diff:

    $ git config --global alias.cdiff 'difftool -y -x "diff -c"'
    $ git cdiff | less
    Git Baby is a git and github fan, let's start git clone.