Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git-diff create a "context" format diff?

Tags:

git

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:

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.)

like image 418
Greg Hewgill Avatar asked Jun 28 '10 00:06

Greg Hewgill


People also ask

What is git diff format?

By default, git diff command options will display the unified diff format between two commits. The combined diff format shows two or more user-specified files with one file and shows how that file is different from each of the specified files. You can use the -c or --cc option to produce a combined diff.

What does git diff -- cached do?

git diff --cached: It shows only those changes of tracked files which are present in staging area. git diff HEAD: It shows all changes of tracked files which are present in working directory and staging area.

Why git diff does not show changes?

Your file is already staged to be committed. You can show it's diff using the --cached option of git. To unstage it, just do what git status suggests in it's output ;) You can check The Git Index For more info.

How do I use the diff command in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.


1 Answers

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
like image 185
Deven T. Corzine Avatar answered Oct 08 '22 05:10

Deven T. Corzine