2

I am trying to show the diff between the last commit and the one before:

git diff HEAD^ HEAD <filename>

but that displays nothing. I know in fact that there is difference between the two commits.

What am I doing wrong and how should I correct it?

P.S.: I feel that this has been discussed many times before but somehow can't find any useful reference.

amphibient
  • 29,770
  • 54
  • 146
  • 240

2 Answers2

5

It would display nothing if that particular file had no changes between HEAD^ and HEAD.

Note that with git1.8.5+, you can do a:

git diff @^ -- afile

(@ means HEAD)


git log -p -- aFile would give all the SHA1 were a change for that file occurred.
(-p for displaying the diff)


To see the last modification on a file (without having to deal with HEAD or other SHA1):

git log -1 -p -- aFile
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • but it does have changes between the latest commit and the one before, which is what i am trying to show – amphibient Feb 08 '14 at 00:24
  • @amphibient does a `git log -- aFile` shows the latest commit and the commit just before (compared to a simple `git log`)? – VonC Feb 08 '14 at 00:24
  • It seems that there is a diff between HEAD and last commit, for this file, according to OP. I wonder if cache may be involved in this behaviour. – axelduch Feb 08 '14 at 00:25
  • @aduch not necessarily: the "last" commit for that file might simply not be the latest commit for the repo. Or the "previous" commit for that file might be an older commit than the actual "previous" commit for the repo. – VonC Feb 08 '14 at 00:26
  • yes, `git log` is showing a history of commits on the particular file. i also thought that the fact i have this particular file staged may have something to do but i also tested on a non-staged, non-changed clean file i never touched and got the same behavior – amphibient Feb 08 '14 at 00:27
  • @amphibient staged doesn't mean committed. Anyway, do compare the first tow SHA1 returned by a `git log` and a `git log -- aFile`: are those two the same? – VonC Feb 08 '14 at 00:28
  • no, they are different because `git log` without a file shows history for the whole branch and this particular file i am going against was not changed within the latest commit in the branch (maybe within the commit five commits ago...) – amphibient Feb 08 '14 at 00:30
  • and as far as "staged doesn't mean committed", i am well aware of that. – amphibient Feb 08 '14 at 00:32
  • @amphibient so you have your answer: `HEAD` and `HEAD^` are not the right SHA1 when it comes to the history of your file: a simply `git log -1 -p -- aFile` will be enough to see the last modification. – VonC Feb 08 '14 at 00:33
  • thanks. is there a way to view that delta with diff(tool)? the thing is, i like using my visual difftool (diffmerge) for visualization. – amphibient Feb 08 '14 at 00:42
  • If you found out the revision where that change wasn’t in yet, then just remember that hash and then you can do `git diff oldcommit HEAD -- file` to show a diff for that file between the old commit and the current HEAD. – poke Feb 08 '14 at 00:44
  • @amphibient http://stackoverflow.com/a/17049263/6309 is a good start, but for a repo only, not a file. http://stackoverflow.com/a/17604584/6309 shows that you need `git log` to get the right SHA1, then you can call difftool. – VonC Feb 08 '14 at 00:46
  • i find it retarded not to be able to use the `difftool` between two previous versions (where neither of the compared versions are the HEAD)... doubly retarded what's more – amphibient Feb 08 '14 at 01:03
0

The changes you made in HEAD in a particular file are obviously not there in HEAD^ otherwise it wouldn't be part of the commit at all.

The command to see those changes is: git show HEAD -- <filename>

yasouser
  • 5,113
  • 2
  • 27
  • 41