Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "git blame" a deleted line?

Tags:

git

git blame is great for modified and added lines, but how can I find when a line that existed in a specific previous commit was eventually deleted. I'm thinking bisect, but I was hoping for something handier.

(Before you ask: in this case, I just did a git log -p and searched through for the code line and (a) some idiot had just deleted the vital line in the previous commit and (b) I was that idiot.)

like image 971
Michael Lorton Avatar asked Dec 10 '10 00:12

Michael Lorton


People also ask

How do I blame in git?

The git blame command is used to examine the contents of a file line by line and see when each line was last modified and who the author of the modifications was. The output format of git blame can be altered with various command line options.

How do I git blame on GitHub?

On GitHub.com, navigate to the main page of the repository. Click to open the file whose line history you want to view. In the upper-right corner of the file view, click Blame to open the blame view.

What is blame annotate in git?

The blame command is a Git feature, designed to help you determine who made changes to a file. Despite its negative-sounding name, git blame is actually pretty innocuous; its primary function is to point out who changed which lines in a file, and why. It can be a useful tool to identify changes in your code.


2 Answers

If you know the contents of the line, this is an ideal use case for:

git log -S <string> path/to/file 

which shows you commits which introduce or remove an instance of that string. There's also the -G<regex> which does the same thing with regular expressions! See man git-log and search for the -G and -S options, or pickaxe (the friendly name for these features) for more information.

The -S option is actually mentioned in the header of the git-blame manpage too, in the description section, where it gives an example using git log -S....

like image 147
Cascabel Avatar answered Sep 21 '22 01:09

Cascabel


I think what you really want is

git blame --reverse START..END filename 

From the manpage:

Walk history forward instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed. This requires a range of revisions like START..END where the path to blame exists in START.

With git blame reverse, you can find the last commit the line appeared in. You still need to get the commit that comes after.

You can use the following command to show a reversed git log. The first commit shown will be the last time that line appears, and the next commit will be when it is changed or removed.

git log --reverse --ancestry-path COMMIT^..master 
like image 34
Chronial Avatar answered Sep 20 '22 01:09

Chronial