1

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 ?

Charles
  • 988
  • 1
  • 11
  • 28

2 Answers2

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
1

You have a couple of options here:

  1. Use the -G option of git log to find the commits that added or removed a line that matches the code that was deleted:
    git log -G"code that was deleted"
    
    where the specified string can also be a regular expression
  2. Use the -L option with a line range to find the commits that modified those lines:
    git log -L10,20:path/to/file
    
    would list the commits that modified code between lines 10 and 20 in path/to/file
  3. Use the -L option with the name of the function that was deleted:
    git log -Lfoo:path/to/file
    
    would list the commits that modified the foo function in path/to/file.

Once you know the commit that deleted that code, you can easily determine the author.

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