3

Let's say we see a line in our code:

a = foo.bar + xyz.abc + fn() * 3;

now we know foo.bar is the "buggy" code or the code causing some issue. And git blame will show that the line is by Michael. However, Michael might not have checked in that part of the code. He might have only altered the * 3 part. So in this case, how could we find the first occurrence of the foo.bar in the repo, and find out who wrote it or added that in?

I thought of using

git show HEAD:path/to/file.js | grep 'foo.bar'

and then

git show HEAD~1:path/to/file.js | grep 'foo.bar'
git show HEAD~2:path/to/file.js | grep 'foo.bar'

and just increase the 2 to 3, 4, etc, until I don't see that line showing the foo.bar.

But what if the number can be big, like 120 or 343. Is there a better way or faster way?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    Possible duplicate of [Finding a Git commit that introduced a string in any branch](http://stackoverflow.com/questions/5816134/finding-a-git-commit-that-introduced-a-string-in-any-branch) – Biffen Mar 16 '16 at 16:32
  • use pickaxe Possible dublicate https://stackoverflow.com/questions/4468361/search-all-of-git-history-for-a-string – Serge Aug 01 '19 at 20:45

2 Answers2

1

You can use blame with commit sha to continue blaming deeper, for example:

git blame -L65,+2 -- application.py
# the commit sha in the output is 4dd5a167
# 4dd5a167 (Boris Serebrov 2015-09-08 21:33:20 +0300 65)     except database.ItemNotFound:
# 74311d17 (Boris Serebrov 2015-11-12 11:16:50 +0200 66)         # no node - log error and create it

# continue blaming from the 4dd5a167's parent (4dd5a167^)
git blame -L65,+2 4dd5a167^ -- application.py

This feature is used in the vim-fugitive vim plugin and it is often very useful. You can do :Gblame and then press ~ to "re-blame" the change. Even if you are not a vim user, it is worth trying to see how it works. Here is a good explanation about what fugitive does.

Community
  • 1
  • 1
Borys Serebrov
  • 15,636
  • 2
  • 38
  • 54
0

I guess what you're trying to do can be done with the command git-log with the option --grep-reflog.

Since you already know the file you're interested in, I guess the list will be short. With some options well used, I guess all you want can be done with git-log.

git-log Documentation page

MessuKilkain
  • 76
  • 1
  • 1
  • 11