I need to know who and when deleted some portion of a source file using git. Unfortunately, it seems git blame won't directly show me that, as said section is totally absent from file in the current revision. Any suggestions ?
Asked
Active
Viewed 139 times
2 Answers
4
You can find commits which were changing something in lines having certain string. This way you can see which commit deleted some line if you know what its content was
git log -S <string> path/to/file
In order to find how exactly these lines were written you can see your file in some very old commit:
git show old-commit-hash^:src/path/to/file

Charles
- 988
- 1
- 11
- 28

Łukasz Ślusarczyk
- 1,775
- 11
- 20
-
Very useful option in general. – Charles Aug 24 '20 at 10:48
1
You have a couple of options here:
- Use the
-G
option ofgit log
to find the commits that added or removed a line that matches the code that was deleted:
where the specified string can also be a regular expressiongit log -G"code that was deleted"
- Use the
-L
option with a line range to find the commits that modified those lines:
would list the commits that modified code between linesgit log -L10,20:path/to/file
10
and20
inpath/to/file
- Use the
-L
option with the name of the function that was deleted:
would list the commits that modified thegit log -Lfoo:path/to/file
foo
function inpath/to/file
.
Once you know the commit that deleted that code, you can easily determine the author.

Enrico Campidoglio
- 56,676
- 12
- 126
- 154