0

I'm interested in finding a commit in git that deleted a line by identifying the earliest commit which did not include it.

I'm aware of the git blame --reverse command but it shows the last commit including the line, which is not the same. I tried using that combined with git log --reverse --ancestry-path <commit shown by blame>..HEAD to find the commit I needed with some success, but it turned out tedious in certain cases.

Say I have the following graph

  /----G---H---I
 /      \   \   \
A----B---C---D---F--G
  \ /
   X

I know that A contains the line but G does not. The commit that actually deleted it was X. However, if I run git blame --reverse A..G, it will tell me that the last commit with that line was I. By doing git log --reverse --ancestry-path I..G, I will see that F was a merge so I will need to dig deeper by running git blame --reverse A..D, which will return H. Then I'll find G, only then finally A, which will allow me to find X.

It's not an unlikely scenario, I had this problem when we had a lot of pull requests merged into the main branch at a similar time, all branched off commits that still contained the deleted code.

Is there a command that would return the commit X straight away? Or at least is there a quicker way of finding it?

kamilk
  • 3,829
  • 1
  • 27
  • 40
  • Possible duplicate of [How do I "git blame" a deleted line?](http://stackoverflow.com/questions/4404444/how-do-i-git-blame-a-deleted-line) – smarber Mar 10 '17 at 13:20

1 Answers1

1

git log -S"this is part of the line that is added or removed" will give you all commits that change the number of occurrences of the specified string (i.e. addition/deletion) in a file.

You can also make that string treated as a regex with --pickaxe-regex.

Vampire
  • 35,631
  • 4
  • 76
  • 102