7

There is a file in my git repo which was changed a lot of times. I want to know the revisions in which a specific text was present in this file.

Is there a git command to get this information?

Something like:

git find "specific_text" -- /frequently/modified/file

Which would output a list of commits.

jeremy-george
  • 1,171
  • 1
  • 9
  • 20
  • Does this answer your question? [How to grep (search) committed code in the Git history](https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history) – Luuk Oct 15 '20 at 13:07
  • It is not called `find`, it is called `grep`. Type `git help grep` in your terminal or read its [documentation on the Git website](https://git-scm.com/docs/git-grep). – axiac Oct 15 '20 at 13:17

2 Answers2

10

git grep <pattern> will search only one commit.

If you want to search all of git's history, use one of the pickaxe options :

git log -G "specific_text" -- frequently/modified/file
# or :
git log -S "specific_text" -- frequently/modified/file

The difference between the two is explained in the docs :

  • -G will list all commits where specific_text appears in the diff,
  • -S will only list commits where the count of specific_text changes, e.g : the commit where this text appears, the commit where it gets deleted, but not a commit which just moves it around, without adding or removing an instance.

You can also add -p if you want to see the diff itself :

git log -p -S "specific_text" -- frequently/modified/file
LeGEC
  • 46,477
  • 5
  • 57
  • 104
1

You can do

git log -G 'specific text' --  /frequently/modified/file
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65