git diff, with external diff program, just once

Solution 1:

I think you might be looking for something like

git show somecommit:path/to/file | mydiff - path/to/file

or

mydiff  <(git show somecommit:path/to/file) \
        <(git show someothercommit:path/to/file)

but a little manpage reading says difftool does accept path limiters, and also has the -x option, so I don't see any problem there. For the case in your comment I'd use

git difftool -yx mydiff @ -- file

Solution 2:

The integrated way in git to open a diff in an external viewer is git difftool.

As far as cli options go, git difftool takes the same arguments as git diff, and opens the resulting diff in the external viewer.
One extra useful option is -d | --dir-diff, which will create two checkouts of your repo in a temporary directory and open the external viewer in "directory comparison mode" :

git difftool -d <something> <something else>

As far as the choice for external tool goes :

  • you can configure a default difftool : git config diff.tool kdiff3
  • you can select any tool you want on a one of basis using -t | --tool :
git difftool -t meld <something> <something else>
  • you can also define custom commands by creating difftool.<tool>.cmd entries in your git configuration

quoting the docs of the -t|--tool section :

You can explicitly provide a full path to the tool by setting the configuration variable difftool.<tool>.path. For example, you can configure the absolute path to kdiff3 by setting difftool.kdiff3.path. Otherwise, git difftool assumes the tool is available in PATH.

Instead of running one of the known diff tools, git difftool can be customized to run an alternative program by specifying the command line to invoke in a configuration variable difftool.<tool>.cmd.

When git difftool is invoked with this tool (either through the -t or --tool option or the diff.tool configuration variable) the configured command line will be invoked with the following variables available: $LOCAL is set to the name of the temporary file containing the contents of the diff pre-image and $REMOTE is set to the name of the temporary file containing the contents of the diff post-image. $MERGED is the name of the file which is being compared. $BASE is provided for compatibility with custom merge tool commands and has the same value as $MERGED.