197

In HEAD (the latest commit), I have a file named foo. In my current working tree, I renamed it to bar, and also edited it.

I want to git diff foo in HEAD, and bar in my current working tree.

MiJyn
  • 4,737
  • 4
  • 36
  • 60
  • 101
    I thought this question (from the title) might be about using git diff on two files that aren't necessarily in a repo. I found that the --no-index flag is for that, e.g. `git diff --no-index --word-diff old_file.txt new_file.txt` (--word-diff highlights changes by word, not just line, which is super helpful for long text). – Pat Nov 18 '14 at 19:04

3 Answers3

223

Specify the paths explicitly:

git diff HEAD:full/path/to/foo full/path/to/bar

Check out the --find-renames option in the git-diff docs.

Credit: twaggs.

DannyDannyDanny
  • 618
  • 7
  • 24
Steven Wexler
  • 15,429
  • 7
  • 46
  • 80
  • 44
    I want to mention, that you could compare any two files using `git diff ` even if they are not in a git repository. – mitenka Nov 10 '17 at 15:09
  • 9
    @mitenka I don't think that's correct. If you look at `git diff --help` you'll see that the only patterns that are supported for two files is `git diff [] --no-index [--] `. `git diff a b` only matches commit patterns, not files. – Doug Jul 08 '20 at 05:34
  • 3
    @Doug Here is the quote from help command you mentioned, https://git-scm.com/docs/git-diff: Show [...] changes between two files on disk. – mitenka Jul 22 '20 at 23:11
  • @mitenka the help says, unless you use '--no-index' it does not compare files; your comment should be a separate answer; it's not true and it's not relevant to this answer. – Doug Jul 23 '20 at 02:09
  • 9
    @Doug Thank you for your opinion. Here's the quote from help you reference to: You can omit the `--no-index` option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git. – mitenka Jul 23 '20 at 10:07
  • What does the `HEAD:` signify? Also, is there way how to diff between files of two different branches? – paradocslover Apr 30 '21 at 07:20
  • 1
    using the above without the `HEAD:` worked for me – Anupam Dec 06 '21 at 09:37
210

I believe using --no-index is what you're looking for:

git diff [<options>] --no-index [--] <path> <path>

as mentioned in the git manual:

This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.

mh sattarian
  • 2,326
  • 1
  • 9
  • 13
4

If you are using tortoise git you can right-click on a file and git a diff by: Right-clicking on the first file and through the tortoisegit submenu select "Diff later" Then on the second file you can also right-click on this, go to the tortoisegit submenu and then select "Diff with yourfilenamehere.txt"

camjocotem
  • 345
  • 6
  • 16