232

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.

2
  • 114
    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, 2014 at 19:04
  • Very similar question. This question focuses on when one file (at least) is in the working tree, and the other focuses on when both files have been committed. Dec 13, 2022 at 23:12

3 Answers 3

253

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.

1
  • 3
    Quite helpful for leveraging git's powerful whitespace-insensitive "word diff" (eg: git diff --no-index --word-diff file1 file2), even for files that are not git-controlled. (At least for me on git v2.25.1, Ubuntu v20.04.) Dec 11, 2022 at 18:09
247

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.

git diff <path> <path> will compare two working-tree files as long as at least one of them is not in a git repo or the command is run from outside of a git repo. If you want to ensure git knows you are only comparing files in the working-tree (that is, on files in your directory rather than files added or commited to git), use git --no-index <path> <path>

11
  • 62
    I want to mention, that you could compare any two files using git diff <path> <path> even if they are not in a git repository.
    – mitenka
    Nov 10, 2017 at 15:09
  • 10
    @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 [<options>] --no-index [--] <path> <path>. git diff a b only matches commit patterns, not files.
    – Doug
    Jul 8, 2020 at 5:34
  • 3
    @Doug Here is the quote from help command you mentioned, git-scm.com/docs/git-diff: Show [...] changes between two files on disk.
    – mitenka
    Jul 22, 2020 at 23:11
  • 10
    @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, 2020 at 10:07
  • 1
    using the above without the HEAD: worked for me
    – Anupam
    Dec 6, 2021 at 9:37
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.