244

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 between foo in HEAD, and bar in my current working tree.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
MiJyn
  • 5,327
  • 4
  • 37
  • 64
  • 116
    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
  • [Very similar question](https://stackoverflow.com/questions/8131135/git-how-to-diff-two-different-files-in-different-branches). 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. – Josiah Yoder Dec 13 '22 at 23:12

3 Answers3

268

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,936
  • 1
  • 11
  • 13
  • 4
    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.) – Johnny Utahh Dec 11 '22 at 18:09
255

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>

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80
  • 64
    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
  • 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 [] --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
  • 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 '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
  • 2
    using the above without the `HEAD:` worked for me – Anupam Dec 06 '21 at 09:37
  • 1
    @mitenka So to "edit" your original comment (if that were allowed), we should say that `git diff ` can be used, but **only** if at least one of the files is not in a git repo. – Josiah Yoder Dec 13 '22 at 23:09
  • Good point @josiah-yoder. I can't edit comments tho ‍♂️ – mitenka Dec 14 '22 at 00:05
  • @mitenka But I now have queue-free priveleges, so I've added an edited version of the answer to the question itself. Now back to paying work. – Josiah Yoder Dec 14 '22 at 21:28
  • I'm not enough of an expert to comment on your original intent in this answer. I'm a bit confused as to when your answer compares with the file tree. I'm certain you are naming a file in a commit/branch with the ':' syntax, but don't follow how this will compare with a file in the working tree. – Josiah Yoder Dec 14 '22 at 21:31
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
  • 375
  • 5
  • 18