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?