# how to diff versions of a file

5 messages from 2009-02-03 to 2009-02-03. Participants: Matt Graham, david@lang.hm, Junio C Hamano, Jeff King.
Thread: https://gitlist.dev/t/17520

## Matt Graham, 2009-02-03 05:00

Subject: how to diff versions of a file
Message-ID: <1c5969370902022100h7c03bbb6h2d9f443f877d89ec@mail.gmail.com>
URL: https://gitlist.dev/e/1c5969370902022100h7c03bbb6h2d9f443f877d89ec%40mail.gmail.com

```
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, 2009-02-03 06:08

Subject: Re: how to diff versions of a file
Message-ID: <alpine.DEB.1.10.0902022206060.28633@asgard.lang.hm>
URL: https://gitlist.dev/e/alpine.DEB.1.10.0902022206060.28633%40asgard.lang.hm
In-Reply-To: <1c5969370902022100h7c03bbb6h2d9f443f877d89ec@mail.gmail.com>

```
On Tue, 3 Feb 2009, Matt Graham wrote:

> 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, 2009-02-03 06:34

Subject: Re: how to diff versions of a file
Message-ID: <7vk588do5q.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vk588do5q.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.DEB.1.10.0902022206060.28633@asgard.lang.hm>

```
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...

```

## Jeff King, 2009-02-03 07:34

Subject: Re: how to diff versions of a file
Message-ID: <20090203073450.GE21367@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20090203073450.GE21367%40sigill.intra.peff.net
In-Reply-To: <1c5969370902022100h7c03bbb6h2d9f443f877d89ec@mail.gmail.com>

```
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

```

## Matt Graham, 2009-02-03 13:48

Subject: Re: how to diff versions of a file
Message-ID: <1c5969370902030548g284e8ae2i7dc3a866c4337a5f@mail.gmail.com>
URL: https://gitlist.dev/e/1c5969370902030548g284e8ae2i7dc3a866c4337a5f%40mail.gmail.com
In-Reply-To: <7vk588do5q.fsf@gitster.siamese.dyndns.org>

```
On Tue, Feb 3, 2009 at 01:34, Junio C Hamano <gitster@pobox.com> wrote:
> 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.

```
