How to Find a Deleted Line in Git
This tutorial provides several solutions to when the line once existed in a specified previous git commit was deleted. Choose the right answer for you.
The git blame command shows the last author who changed a line. But how can we find out who deleted the whole line? Let’s explore the commands that can help to solve this problem.
Find the commits containing the content of the deleted line
If you know the content of the deleted line, you should run git log to show the commits it is included in.
git log
git log -S <string> path/to/fileTo get a more descriptive view, you can add the <kbd class="highlighted">-p</kbd> option.
Instead of a string, you can use regular expression by changing <kbd class="highlighted">-S</kbd> to <kbd class="highlighted">-G</kbd> as follows:
git log with regex
git log -G <regex> path/to/fileFind the latest commit, containing the content of the deleted line
The <kbd class="highlighted">git blame</kbd> command offers a <kbd class="highlighted">--reverse</kbd> option, which processes a commit range backwards to show the last modification before the line was deleted. To find the latest commit that contains the deleted line, identify a commit where the line was present. Then, set that commit as the start point of the range, and <kbd class="highlighted">HEAD</kbd> as the end point.
git blame
git blame --reverse <sha1-commit-hash>..HEAD path/to/fileThis shows the latest revision in which the line existed.