threads / discuss / 17520

how to diff versions of a file

Subject: how to diff versions of a file

## tl;dr

5 messages between Feb 3, 2009 and Feb 3, 2009.

replies: 4people: 4as markdown or json

Matt Graham· Feb 3, 2009, 05:00 UTC · lore

Hi, I'm curious if there's a way to diff between 2 arbitrary versions of a file.

For example, how can I see the diff between tree.c on HEAD and from 3 edits (of tree.c) ago?

I can do git log and then manually use commit names to do the diff, but I'm hoping there's a single command that will let me do that.

Thanks, Matt

david@lang.hm· Feb 3, 2009, 06:08 UTC · re: Matt Graham · lore

Re: how to diff versions of a file

On Tue, 3 Feb 2009, Matt Graham wrote:
Show 8 quoted lines
> Hi,
> I'm curious if there's a way to diff between 2 arbitrary versions of a file.
>
> For example, how can I see the diff between tree.c on HEAD and from 3
> edits (of tree.c) ago?
>
> I can do git log and then manually use commit names to do the diff,
> but I'm hoping there's a single command that will let me do that.
I don't think it's trivial
If you just wanted three commits ago you could do
git diff HEAD..HEAD^^^ tree.c

but three changes to tree.c ago I think you need to do multiple iterations of git blame to find out what commit was three edits ago.

after you have the commit for three edits ago you do git diff HEAD..COMMITID tree.c

David Lang
Junio C Hamano· Feb 3, 2009, 06:34 UTC · re: david@lang.hm · lore

Re: how to diff versions of a file

david@lang.hm writes:
> I don't think it's trivial
I think you meant to say it _is_ trivial.
> If you just wanted three commits ago you could do
>
> git diff HEAD..HEAD^^^ tree.c

That would give you a reverse diff; I think you probably meant the other way around, "git diff HEAD~3 HEAD tree.c".

> but three changes to tree.c ago I think you need to do multiple
> iterations of git blame to find out what commit was three edits ago.
git diff $(git rev-list -3 HEAD -- tree.c | sed -n -e '$p') HEAD -- tree.c

This assumes that the history is linear but your statement "three edits ago" already assumes that, so...

Matt Graham· Feb 3, 2009, 13:48 UTC · re: Junio C Hamano · lore

Re: how to diff versions of a file

On Tue, Feb 3, 2009 at 01:34, Junio C Hamano <gitster@pobox.com> wrote:
Show 20 quoted lines
> david@lang.hm writes:
>
>> I don't think it's trivial
>
> I think you meant to say it _is_ trivial.
>
>> If you just wanted three commits ago you could do
>>
>> git diff HEAD..HEAD^^^ tree.c
>
> That would give you a reverse diff; I think you probably meant the other
> way around, "git diff HEAD~3 HEAD tree.c".
>
>> but three changes to tree.c ago I think you need to do multiple
>> iterations of git blame to find out what commit was three edits ago.
>
> git diff $(git rev-list -3 HEAD -- tree.c | sed -n -e '$p') HEAD -- tree.c
>
> This assumes that the history is linear but your statement "three edits
> ago" already assumes that, so...
Thanks Junio, this is exactly what I was asking for.
And thanks for the tip about it depending on the history being linear.
Jeff King· Feb 3, 2009, 07:34 UTC · re: Matt Graham · lore

Re: how to diff versions of a file

On Tue, Feb 03, 2009 at 12:00:51AM -0500, Matt Graham wrote:
> I'm curious if there's a way to diff between 2 arbitrary versions of a file.
> 
> For example, how can I see the diff between tree.c on HEAD and from 3
> edits (of tree.c) ago?

Others have suggested diffing between two arbitrary revisions, and limiting the diff by path. E.g.:

  git diff HEAD~3 HEAD -- tree.c

which works just fine. But I find it more intuitive to simply specify the diff between two blobs (or trees, if you want), like:

  git diff HEAD~3:tree.c HEAD:tree.c

The added bonus is that you can also use this syntax to diff two files with different names.

-Peff

← back to recent threads