285

Is it possible to get Git to produce a diff between a specific file as it exists now, and as it existed before the last commit that changed it?

That is, if we know:

git log --oneline myfile 

Output:

123abc Fix some stuff 456def Frobble the foos 789dba Initial commit 

Then git diff 456def myfile shows the last change to myfile. Is is possible to do the same without the knowledge produced by the git log; what changed in 123abc?

3
  • 17
    I prefer using git diff HEAD^ <file_path> Commented Sep 12, 2017 at 12:50
  • 10
    @asgs - Doesn't do what I was asking (for two reasons - HEAD^ is 123abc, HEAD^^ is 456def; and if there were other commits that didn't affect this file then HEAD^ refers to them) Commented Sep 12, 2017 at 16:23
  • You're right, missed the "the last commit that changed it" part Commented Sep 13, 2017 at 7:36

3 Answers 3

279

This does exist, but it's actually a feature of git log:

git log -p [-m] [--follow] [-1] <path> 

Note that -p can also be used to show the inline diff from a single commit:

git log -p -1 <commit> 

Options used:

  • -p (also -u or --patch) is hidden deeeeeeeep in the git-log man page, and is actually a display option for git-diff. When used with log, it shows the patch that would be generated for each commit, along with the commit information—and hides commits that do not touch the specified <path>. (This behavior is described in the paragraph on --full-diff, which causes the full diff of each commit to be shown.)
  • -m causes merge commits to include the diff content (otherwise these just show the commit message, as if -p were not specified).
  • -1 shows just the most recent change to the specified file (-n 1 can be used instead of -1); otherwise, all non-zero diffs of that file are shown.
  • --follow is required to see changes that occurred prior to a rename.

As far as I can tell, this is the only way to immediately see the last set of changes made to a file without using git log (or similar) to either count the number of intervening revisions or determine the hash of the commit.

To see older revisions changes, just scroll through the log, or specify a commit or tag from which to start the log. (Of course, specifying a commit or tag returns you to the original problem of figuring out what the correct commit or tag is.)

Credit where credit is due:

  • I discovered log -p thanks to this answer.
  • Credit to FranciscoPuga and this answer for showing me the --follow option.
  • Credit to ChrisBetti for mentioning the -n 1 option and atatko for mentioning the -1 variant.
  • Credit to sweaver2112 for getting me to actually read the documentation and figure out what -p "means" semantically.
  • Credit to Oscar Scholten for pointing out that by default, -p does not show diff-contents for merge commits.
Sign up to request clarification or add additional context in comments.

12 Comments

This was a great solution for me. Showed each commit and its differences to the current file when I ran git log -p filename
Perfect. To see just the last change, it's as simple as adding the -n 1 parameter. git log -p -n 1 filename
-n 1 can also be replaced by -1, it doesn't change the result I just prefer the syntax: git log -p -1 filename
@sweaver2112 As you can tell from the answer itself, I'm happy to incorporate new information into my answer from helpful comments. Instead of complaining, why not suggest an improvement? You can even just edit the answer.
there is a useful option "--skip=[n]". You can type git log -p -1 --skip=1 <path> to display second commit.
|
239

One of the ways to use git diff is:

git diff <commit> <path> 

And a common way to refer one commit of the last commit is as a relative path to the actual HEAD. You can reference previous commits as HEAD^ (in your example this will be 123abc) or HEAD^^ (456def in your example), etc ...

So the answer to your question is:

git diff HEAD^^ myfile 

14 Comments

Oh, of course. I tried HEAD^, but of course that produced nothing. Didn't think to try HEAD^^.
Maybe easier syntax for long-ago commits : HEAD~2
It is not true (at least for Git 1.9.0) that HEAD^^ myfile will actually refer to the second-to-last commit that changed myfile; it will refer to the second-to-last commit overall. Is there any way to specify "I want to see the last change made to this file" without either specifying (part of) the commit hash or counting the number of commits between the last change made to that file and the current revision?
downvote reason: the question asks "between a specific file as it exists now, and as it existed before the last commit that changed it", but if the file was not changed in the last overall commit, this answer does not work.
Why is this upvoted? It it no way answers the question asked >:(
|
9

If you are fine using a graphical tool this works very well:

gitk <file> 

gitk now shows all commits where the file has been updated. Marking a commit will show you the diff against the previous commit in the list. This also works for directories, but then you also get to select the file to diff for the selected commit. Super useful!

1 Comment

Also very useful: git difftool HEAD^ file or git difftool -d HEAD^ path

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.