How to Find a Deleted Line in Git

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 -S <string> path/to/file

To get a more descriptive view, you can add the -c option.

Instead of a string, you can use regular expression by changing -S to -G as follows:

git log -G <regex> path/to/file

Find the latest commit, containing the content of the deleted line

The git blame command offers a --reverse option, which receives a range of commits and shows the latest commit that included the line before its deletion. To find the latest commit that contains the deleted line, you need to find a commit the line was exactly included in. Then, you should set the commit that included the line, as the start point of the range, and the HEAD as the end point.

git blame --reverse <sha1-commit-hash>..HEAD path/to/file

This shows the latest revision in which the line existed in.

The git blame Command

Git suggests a great tool, namely git blame, which explores the file history and finds out the last author who changed a line of the file. The git blame --reverse command gets close to where the line is deleted. It doesn't point to the revision where the line is deleted but leads to the last revision where the line was present.

The git log command

The git log command is a Git tool which is used to examine a repository's history and find a particular version of a project. It displays the committed snapshots. It lists and filters the project history and searches for specific changes. The git log only works on the committed history in comparison with git status, which also works on the working directory and the staging area.