# Which file is older in history?

7 messages from 2013-03-20 to 2013-03-21. Participants: Ramkumar Ramachandra, Junio C Hamano, Jeff King.
Thread: https://gitlist.dev/t/33241

## Ramkumar Ramachandra, 2013-03-20 20:21

Subject: Which file is older in history?
Message-ID: <CALkWK0nTjoR9qBY-hnkrDKnyf8088ZDJ1LK_zNqAoAB7ndvaww@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0nTjoR9qBY-hnkrDKnyf8088ZDJ1LK_zNqAoAB7ndvaww%40mail.gmail.com

```
Hi,

I want to compare two files in a git repository and tell which one was
introduced into the repository earlier, assuming that they're in the
same history line (by ancestry, not timestamp).  The naive way to do
this is to find the individual commits that introduced the files
(`rev-list HEAD -- <filename> | tail -n 1`), and query whether there
are commits between them (`rev-list commit1 ^commit2`). However, this
is slow.  What about bisecting the history until we find a tree that
contains one file but not the other?

Thanks.

Ram

```

## Junio C Hamano, 2013-03-20 20:46

Subject: Re: Which file is older in history?
Message-ID: <7vobeddcjq.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vobeddcjq.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CALkWK0nTjoR9qBY-hnkrDKnyf8088ZDJ1LK_zNqAoAB7ndvaww@mail.gmail.com>

```
Ramkumar Ramachandra <artagnon@gmail.com> writes:

> I want to compare two files in a git repository and tell which one was
> introduced into the repository earlier, assuming that they're in the
> same history line (by ancestry, not timestamp).  The naive way to do
> this is to find the individual commits that introduced the files
> (`rev-list HEAD -- <filename> | tail -n 1`), and...

This must be a trick question but the naïve way I think of is

	git log --diff-filter=A -- path1 path2

```

## Ramkumar Ramachandra, 2013-03-21 11:59

Subject: Re: Which file is older in history?
Message-ID: <CALkWK0nRgzNYViKSx97OyHVOQmFN61Ex9EUvr1JK-0=GX=uX0w@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0nRgzNYViKSx97OyHVOQmFN61Ex9EUvr1JK-0%3DGX%3DuX0w%40mail.gmail.com
In-Reply-To: <7vobeddcjq.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Ramkumar Ramachandra <artagnon@gmail.com> writes:
>
>> I want to compare two files in a git repository and tell which one was
>> introduced into the repository earlier, assuming that they're in the
>> same history line (by ancestry, not timestamp).  The naive way to do
>> this is to find the individual commits that introduced the files
>> (`rev-list HEAD -- <filename> | tail -n 1`), and...
>
> This must be a trick question but the naïve way I think of is
>
>         git log --diff-filter=A -- path1 path2

Thanks, I didn't know about --diff-filter.  I'll need one extra step
to figure out which commit corresponds to the introduction of which
file, no?

```

## Jeff King, 2013-03-21 12:21

Subject: Re: Which file is older in history?
Message-ID: <20130321122130.GA22607@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20130321122130.GA22607%40sigill.intra.peff.net
In-Reply-To: <CALkWK0nRgzNYViKSx97OyHVOQmFN61Ex9EUvr1JK-0=GX=uX0w@mail.gmail.com>

```
On Thu, Mar 21, 2013 at 05:29:39PM +0530, Ramkumar Ramachandra wrote:

> > This must be a trick question but the naïve way I think of is
> >
> >         git log --diff-filter=A -- path1 path2
> 
> Thanks, I didn't know about --diff-filter.  I'll need one extra step
> to figure out which commit corresponds to the introduction of which
> file, no?

Maybe

  git log --format=%H --name-status --diff-filter=A -- path1 path2 |
  perl -lne '
    if (/[0-9a-f]{40}/) { $commit = $& }
    elsif (/^A\t(.*)/) { $h{$1} = $commit }
    END { print "$h{$_} $_" for keys(%h) }
  '

?

-Peff

```

## Jeff King, 2013-03-21 12:24

Subject: Re: Which file is older in history?
Message-ID: <20130321122428.GB22607@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20130321122428.GB22607%40sigill.intra.peff.net
In-Reply-To: <20130321122130.GA22607@sigill.intra.peff.net>

```
On Thu, Mar 21, 2013 at 08:21:30AM -0400, Jeff King wrote:

> On Thu, Mar 21, 2013 at 05:29:39PM +0530, Ramkumar Ramachandra wrote:
> 
> > > This must be a trick question but the naïve way I think of is
> > >
> > >         git log --diff-filter=A -- path1 path2
> > 
> > Thanks, I didn't know about --diff-filter.  I'll need one extra step
> > to figure out which commit corresponds to the introduction of which
> > file, no?
> 
> Maybe
> 
>   git log --format=%H --name-status --diff-filter=A -- path1 path2 |
>   perl -lne '
>     if (/[0-9a-f]{40}/) { $commit = $& }
>     elsif (/^A\t(.*)/) { $h{$1} = $commit }
>     END { print "$h{$_} $_" for keys(%h) }
>   '

Actually, I only looked at your question, not the original point, which
was not which commit was which, but which one was older. If you just
want to know which is older, then just:

  git log --format=%H --name-status --diff-filter=A -- path1 path2 |
  grep ^A |
  tail -1

which uses the regular traversal order (i.e., mostly following commit
timestamps). You can use --topo-order if you want to make sure it
follows the graph structure.

-Peff

```

## Ramkumar Ramachandra, 2013-03-21 12:32

Subject: Re: Which file is older in history?
Message-ID: <CALkWK0nOzUXk0cFh8GCHc=YqLkRJ2XNFgU_tOsua81ROK99mXw@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0nOzUXk0cFh8GCHc%3DYqLkRJ2XNFgU_tOsua81ROK99mXw%40mail.gmail.com
In-Reply-To: <20130321122428.GB22607@sigill.intra.peff.net>

```
Jeff King wrote:
> On Thu, Mar 21, 2013 at 08:21:30AM -0400, Jeff King wrote:
>
>> On Thu, Mar 21, 2013 at 05:29:39PM +0530, Ramkumar Ramachandra wrote:
>>
>> > > This must be a trick question but the naïve way I think of is
>> > >
>> > >         git log --diff-filter=A -- path1 path2
>> >
>> > Thanks, I didn't know about --diff-filter.  I'll need one extra step
>> > to figure out which commit corresponds to the introduction of which
>> > file, no?
>>
>> Maybe
>>
>>   git log --format=%H --name-status --diff-filter=A -- path1 path2 |
>>   perl -lne '
>>     if (/[0-9a-f]{40}/) { $commit = $& }
>>     elsif (/^A\t(.*)/) { $h{$1} = $commit }
>>     END { print "$h{$_} $_" for keys(%h) }
>>   '
>
> Actually, I only looked at your question, not the original point, which
> was not which commit was which, but which one was older. If you just
> want to know which is older, then just:
>
>   git log --format=%H --name-status --diff-filter=A -- path1 path2 |
>   grep ^A |
>   tail -1

Great!  I just learnt about --name-status now.
Nit: tail -<n> is deprecated in favor of tail -n <n>, I think.  It's
nicer to have definite fixed command line options, as opposed to
parsing an arbitrary -(*) and deciding if \1 is a \d+.

```

## Jeff King, 2013-03-21 13:26

Subject: Re: Which file is older in history?
Message-ID: <20130321132620.GB23162@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20130321132620.GB23162%40sigill.intra.peff.net
In-Reply-To: <CALkWK0nOzUXk0cFh8GCHc=YqLkRJ2XNFgU_tOsua81ROK99mXw@mail.gmail.com>

```
On Thu, Mar 21, 2013 at 06:02:32PM +0530, Ramkumar Ramachandra wrote:

> >   git log --format=%H --name-status --diff-filter=A -- path1 path2 |
> >   grep ^A |
> >   tail -1
> 
> Great!  I just learnt about --name-status now.

You can also use "--name-only", but the parsing is a little less robust.

> Nit: tail -<n> is deprecated in favor of tail -n <n>, I think.  It's
> nicer to have definite fixed command line options, as opposed to
> parsing an arbitrary -(*) and deciding if \1 is a \d+.

Yeah, POSIX has always specified "-n" for tail, as well as for head.  I
remember in the late 90's and early 00's running into systems where
POSIX "head -n" did not work, and you _had_ to use "head -1". I don't
recall now if that was the case for tail, too, nor what system that was
on (but if I had to guess, I'd say pre-Solaris SunOS).

So yeah. Using "tail -n 1" is probably a better idea in the long run.

-Peff

```
