246

So if I have a file called foo.rb and it is giving me an error for a missing method called bar, so I want to search the history of foo.rb for the string bar to see if it was ever defined in the past.

I found this Search all of Git history for a string?

But this searches all files. I just want to search in one file.

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422

5 Answers5

334

For this purpose you can use the -S option to git log:

git log -S'bar' -- foo.rb
rtn
  • 127,556
  • 20
  • 111
  • 121
  • 69
    Or `git log -G'bar' -- foo.rb` to search for diff that contains 'bar' rather than for commits that changed number of occurences of 'bar' (see git-log manpage). – Jakub Narębski Apr 18 '12 at 19:16
  • 5
    It didn't work for me with the quotes, I had to use `-Sbar` for it to search for _bar_. Maybe it has something to do with me using the Windows command line. – zbr Feb 17 '17 at 14:26
  • 1
    in other words, `-G` is for a regex search... this is a great reference, too: https://stackoverflow.com/a/4472267/2586761 – ptim Mar 24 '17 at 04:49
  • 1
    to the future me coming here on windows 11 using absolute path to the file did not work. just go to where the file is and use the command above – Nasreddine Galfout Aug 30 '22 at 15:51
  • In Windows 11 use `"` instead of `'`. – CoolMind Nov 11 '22 at 08:19
27

Or maybe you can try this one (from related questions Search all of git history for string)

git rev-list --all foo.rb | (
    while read revision; do
        git grep -F 'bar' $revision foo.rb
    done
)

It will actually look for file content and not commit messages/patches for any occurence of bar.

Community
  • 1
  • 1
Antoine Pelisse
  • 12,871
  • 4
  • 34
  • 34
  • For windows: `for /F %i in ('git rev-list --all foo.rb') git grep -F bar %i foo.rb` (watch for double-quotes if filename or search phrase has spaces) – biscuit314 Oct 09 '19 at 16:32
  • 1
    For me in Windows it didn't work without "do": `for /F %i in ('git rev-list --all foo.rb') do git grep -F bar %i foo.rb` (watch for double-quotes if filename or search phrase has spaces) – xberger Jan 06 '20 at 15:41
5

I used git log -S "string" --follow -p "path of file" which shows the full history of all changes with that string.

stack questions
  • 133
  • 1
  • 8
4

As mentioned by @Jakub Narębski I prefer to regex search for diff that contains 'bar'

git log -G'bar' -- foo.rb
Mustapha-Belkacim
  • 1,653
  • 1
  • 15
  • 19
3

There's an override for git log command (from manual):

$ git log Makefile      # commits that modify Makefile

So you could use:

git log foo.rb | grep "bar"
Dmitry Reznik
  • 6,812
  • 2
  • 32
  • 27
  • 8
    Thanks, but this just searches the commit history, not the history of the file's source. – JD Isaacks Apr 18 '12 at 18:20
  • 2
    There's actually another override that searches for string data change: git log -S'foo()' # commits that add or remove any file data matching the string 'foo()' – Dmitry Reznik Apr 18 '12 at 18:23
  • 1
    You could also try using --full-diff parameter. Logs do not only show the commits history, but the diffs also. – Dmitry Reznik Apr 18 '12 at 18:26
  • @DmitriyReznik - apparently `--full-diff` doesn't work (as in, no diff is shown) without `-p`, at least on git 1.7.9.5 – sdaau Jul 03 '15 at 00:09