Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git blame committed line

Tags:

git

Is it possible somehow for GIT to find which commit introduced a specific line of code in a specific file? This is assuming that there have been many commits since that line was added. Or is this something that must be done in a script while looking at the git blame of all the commits for the file in which the line is present?

To clarify

Original file->Line Added to file and committed -> Many other commits adding other lines and changing the code

like image 565
Gustavo Litovsky Avatar asked Dec 03 '12 21:12

Gustavo Litovsky


People also ask

Does git blame identify a particular commit?

The git blame command is used to know who/which commit is responsible for the latest changes made to a file. The author/commit of each line can also been seen.

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.

How do I exit git blame?

You can also quit the git blame window using the q key on your keyboard.

What is raw and blame in git?

The Raw, Blame, and History buttons appear when viewing a single file of a repository. For example, let's visit the README.md file by clicking on it: The Raw button, like the name suggests, opens the file in a raw form, meaning that any HTML formatting disappears.


3 Answers

You can use git blame -l filename to get the SHA1 hash from when the line was changed.

You also can use --reverse:

--reverse

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 revision like START..END where the path to blame exists in START.

See: http://www.kernel.org/pub/software/scm/git/docs/git-blame.html

like image 57
phschoen Avatar answered Nov 07 '22 01:11

phschoen


It seems you can use git log -L to check file. For example, I want to check the commits for file.c, line from 55 to 60:

git log -L55,60:file.c
like image 33
ct.kuo Avatar answered Nov 07 '22 01:11

ct.kuo


You can blame against the specific code string like this.

git log -S 'THE-CODE' 
like image 42
Ben Wrighton Avatar answered Nov 07 '22 02:11

Ben Wrighton