What does 'git blame' do?

GitGit Blame

Git Problem Overview


I saw a lot of questions about methods of using git blame, but I don't really understand them.

I see a Blame button on top of files on the GitHub interface. Upon clicking it, it shows some diff with usernames on the left bar. What does that indicate?

Why is git blame actually used, apart from GitHub?

Git Solutions


Solution 1 - Git

From git-blame:

> Annotates each line in the given file with information from the revision which last modified the line. Optionally, start annotating from the given revision. > > When specified one or more times, -L restricts annotation to the requested lines.

Example:

[email protected]:~# git blame .htaccess
...
^e1fb2d7 (John Doe 2015-07-03 06:30:25 -0300  4) allow from all
^72fgsdl (Arthur King 2015-07-03 06:34:12 -0300  5)
^e1fb2d7 (John Doe 2015-07-03 06:30:25 -0300  6) <IfModule mod_rewrite.c>
^72fgsdl (Arthur King 2015-07-03 06:34:12 -0300  7)     RewriteEngine On
...

Please note that git blame does not show the per-line modifications history in the chronological sense. It only shows who was the last person to have changed a line in a document up to the last commit in HEAD.

That is to say that in order to see the full history/log of a document line, you would need to run a git blame path/to/file for each commit in your git log.

Solution 2 - Git

It's to figure out which co-worker wrote the specific line or ruined the project, so you can blame them :)

Solution 3 - Git

From GitHub:

> 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.

Basically, git-blame is used to show what revision and author last modified each line of a file. It's like checking the history of the development of a file.

Solution 4 - Git

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.

git blame filename (commits responsible for changes for all lines in code)

git blame filename -L 0,10 (commits responsible for changes from line "0" to line "10")

There are many other options for blame, but generally these could help.

Solution 5 - 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.

> If there was a bug in code,use it to identify who cased it,then you can blame him. Git blame is get blame(d).

If you need to know history of one line code,use git log -S"code here", simpler than git blame.

git log vs git blame

Solution 6 - Git

The git blame command annotates lines with information from the revision which last modified the line, and... with Git 2.22 (Q2 2019), will do so faster, because of a performance fix around "git blame", especially in a linear history (which is the norm we should optimize for).

See commit f892014 (02 Apr 2019) by David Kastrup (fedelibre). (Merged by Junio C Hamano -- gitster -- in commit 4d8c4da, 25 Apr 2019)

> ## blame.c: don't drop origin blobs as eagerly > > When a parent blob already has chunks queued up for blaming, dropping the blob at the end of one blame step will cause it to get reloaded right away, doubling the amount of I/O and unpacking when processing a linear history. > > Keeping such parent blobs in memory seems like a reasonable optimization that should incur additional memory pressure mostly when processing the merges from old branches.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHimanshu MishraView Question on Stackoverflow
Solution 1 - GitMarkView Answer on Stackoverflow
Solution 2 - GitXRayView Answer on Stackoverflow
Solution 3 - GitHimanshu MishraView Answer on Stackoverflow
Solution 4 - GitBharath T SView Answer on Stackoverflow
Solution 5 - GitJackChouMineView Answer on Stackoverflow
Solution 6 - GitVonCView Answer on Stackoverflow