git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Most recent revision that contains a string

From
Josh Triplett <josh@joshtriplett.org>
Date
Aug 21, 2016, 01:30 UTC
Message-ID
<20160821013040.4sfmg2v7rcovxxmb@x>
In-Reply-To
<87d1l3ce40.fsf@thinkpad.rath.org>
On Sat, Aug 20, 2016 at 02:41:35PM -0700, Nikolaus Rath wrote:
Show 9 quoted lines
> 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.

Previous: Nikolaus RathNext: Andreas Schwab
Message 2 of 7 in “Most recent revision that contains a string”
  1. Nikolaus RathAug 20, 2016
  2. Josh TriplettAug 21, 2016
  3. Andreas SchwabAug 21, 2016
  4. Josh TriplettAug 21, 2016
  5. Eric WongAug 21, 2016
  6. Nikolaus RathAug 22, 2016
  7. Junio C HamanoAug 23, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.