# Most recent revision that contains a string

7 messages from 2016-08-20 to 2016-08-23. Participants: Nikolaus Rath, Josh Triplett, Eric Wong, Andreas Schwab, Junio C Hamano.
Thread: https://gitlist.dev/t/43897

## Nikolaus Rath, 2016-08-20 21:41

Subject: Most recent revision that contains a string
Message-ID: <87d1l3ce40.fsf@thinkpad.rath.org>
URL: https://gitlist.dev/e/87d1l3ce40.fsf%40thinkpad.rath.org

```
Hello,

What's the easiest way to find the most recent revision (of any file in
the repository, including those that have been deleted in the current
HEAD) that contains a given string?

I was hoping that "git grep" would do this (like in Mercurial), but as
far as I can tell it only greps through the working copy. Or is there a
trick that I'm missing?

Thanks,
-Nikolaus

-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«

```

## Josh Triplett, 2016-08-21 01:30

Subject: Re: Most recent revision that contains a string
Message-ID: <20160821013040.4sfmg2v7rcovxxmb@x>
URL: https://gitlist.dev/e/20160821013040.4sfmg2v7rcovxxmb%40x
In-Reply-To: <87d1l3ce40.fsf@thinkpad.rath.org>

```
On Sat, Aug 20, 2016 at 02:41:35PM -0700, Nikolaus Rath wrote:
> Hello,
> 
> What's the easiest way to find the most recent revision (of any file in
> the repository, including those that have been deleted in the current
> HEAD) that contains a given string?
> 
> I was hoping that "git grep" would do this (like in Mercurial), but as
> far as I can tell it only greps through the working copy. Or is there a
> trick that I'm missing?

git grep can search through any arbitrary blob, tree, commit, or tag.
So you can search through HEAD~10, or HEAD~10:path/to/directory, or
HEAD~10:path/to/file.  (You can also search the index with --cached, and
various other options exist as well.)

If you want to find a change that introduces or removes a particular
string, you could use "git log -S".  That doesn't allow regexes, but it
might do what you want.  "git grep" will find occurrences of the string
in the current version, and if it has been removed, "git log -S" will
find the removal.

I don't know of any way to do that in one file-oriented step, searching
backward through the first occurrence of every path, including deleted
paths.

```

## Eric Wong, 2016-08-21 02:46

Subject: Re: Most recent revision that contains a string
Message-ID: <20160821024647.GA19536@starla>
URL: https://gitlist.dev/e/20160821024647.GA19536%40starla
In-Reply-To: <87d1l3ce40.fsf@thinkpad.rath.org>

```
Nikolaus Rath <Nikolaus@rath.org> wrote:
> What's the easiest way to find the most recent revision (of any file in
> the repository, including those that have been deleted in the current
> HEAD) that contains a given string?

I normally do something like:

	git log -r --raw -p -SSTRING
	git log -r --raw -p -GREGEXP

You can also add --diff-filter=D to filter only on deletes.


Btw, please don't set these headers on kernel.org lists:

	Mail-Copies-To: never
	Mail-Followup-To: git@vger.kernel.org

Like any mail server, vger fails from time-to-time and
reply-to-all prevents it from being a single point of failure.
Thanks.

```

## Andreas Schwab, 2016-08-21 08:13

Subject: Re: Most recent revision that contains a string
Message-ID: <m2mvk6iloy.fsf@linux-m68k.org>
URL: https://gitlist.dev/e/m2mvk6iloy.fsf%40linux-m68k.org
In-Reply-To: <20160821013040.4sfmg2v7rcovxxmb@x>

```
On Aug 20 2016, Josh Triplett <josh@joshtriplett.org> wrote:

> If you want to find a change that introduces or removes a particular
> string, you could use "git log -S".  That doesn't allow regexes,

It does, actually, see --pickaxe-regex.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Josh Triplett, 2016-08-21 08:48

Subject: Re: Most recent revision that contains a string
Message-ID: <20160821084837.nyfnwefx5hteipcq@x>
URL: https://gitlist.dev/e/20160821084837.nyfnwefx5hteipcq%40x
In-Reply-To: <m2mvk6iloy.fsf@linux-m68k.org>

```
On Sun, Aug 21, 2016 at 10:13:33AM +0200, Andreas Schwab wrote:
> On Aug 20 2016, Josh Triplett <josh@joshtriplett.org> wrote:
> > If you want to find a change that introduces or removes a particular
> > string, you could use "git log -S".  That doesn't allow regexes,
> 
> It does, actually, see --pickaxe-regex.

Thanks; I found that after looking up "git log -G", which Eric Wong
mentioned in another reply.

```

## Nikolaus Rath, 2016-08-22 18:06

Subject: Re: Most recent revision that contains a string
Message-ID: <871t1ghe5s.fsf@thinkpad.rath.org>
URL: https://gitlist.dev/e/871t1ghe5s.fsf%40thinkpad.rath.org
In-Reply-To: <20160821024647.GA19536@starla>

```
On Aug 21 2016, Eric Wong <e@80x24.org> wrote:
> Nikolaus Rath <Nikolaus@rath.org> wrote:
>> What's the easiest way to find the most recent revision (of any file in
>> the repository, including those that have been deleted in the current
>> HEAD) that contains a given string?
>
> I normally do something like:
>
> 	git log -r --raw -p -SSTRING
> 	git log -r --raw -p -GREGEXP
>
> You can also add --diff-filter=D to filter only on deletes.

Great, thanks!

> Btw, please don't set these headers on kernel.org lists:
>
> 	Mail-Copies-To: never
> 	Mail-Followup-To: git@vger.kernel.org
>
> Like any mail server, vger fails from time-to-time and
> reply-to-all prevents it from being a single point of failure.

Huh? If the list-server fails only I will receive the message so it's
still lost for everyone else. But I am more than happy to take this
little risk if it saves me from the nuisance of getting duplicate
responses.

Best,
-Nikolaus

-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«

```

## Junio C Hamano, 2016-08-23 19:36

Subject: Re: Most recent revision that contains a string
Message-ID: <xmqqr39fuvjy.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqr39fuvjy.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <871t1ghe5s.fsf@thinkpad.rath.org>

```
Nikolaus Rath <Nikolaus@rath.org> writes:

>> Btw, please don't set these headers on kernel.org lists:
>>
>> 	Mail-Copies-To: never
>> 	Mail-Followup-To: git@vger.kernel.org
>>
>> Like any mail server, vger fails from time-to-time and
>> reply-to-all prevents it from being a single point of failure.
>
> Huh? If the list-server fails only I will receive the message so it's
> still lost for everyone else. But I am more than happy to take this
> little risk if it saves me from the nuisance of getting duplicate
> responses.

On a mailing list, your readers' time is more valuable than yours,
simply because there are a lot more of them.

I am typing this message ONLY because I want to talk to YOU, and I
want your address to be on its To: field, with Eric and the list on
Cc: field.  The recipients can prioritize their incoming messages by
reading what's addressed directly to them first and when they have
leftover time they can read ones that they are CC'ed, but because
you used Mail-Followup-To: I had to correct the headers manually,
which means you stole time from me.  And if I didn't then your
Mail-Followup-to: would have stolen time from the other recipients.

Please don't use it on this list.


```
