W3docs

git blame

On this page you can find useful information about git blame command, as well as how it works with examples, and see the list of the most common options.

Description

The git blame command is a flexible tool with several options of use. The most important function of the git blame command is the display of author metadata attached to a particular committed line in a file. It is used for exploring the file history and finding out the last author who changed the line.

git blame

How it works

To clearly show how git blame works, let’s consider an example where we have a README.md file with a few commits from different authors.

In the following example, we use git blame. The state of the example repository can be explored with a git log. The commit history looks like this:

git log and git blame

git log
commit 745a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a
Author: Bob Smith <[email protected]m>
Date: Fri Apr 1 19:55:15 2019 +0000
Another commit to help git blame track the who, the what, and the when
commit eb06faedb1fdd159d62e4438fc8dbe9c9fe0728b
Author: Bob Smith <[email protected]m>
Date: Fri Apr 1 19:53:23 2019 +0000
Creating the third commit, along with Nick and Robert, so that Nick can get git blame docs.
commit 990c2b6a84464fee153253dbf02e845a4db372bb
Merge: 82496ea 89feb84
Author: Tom Brown <[email protected]m>
Date: Fri Apr 1 05:33:01 2019 +0000
Merged in tom-brown/git-blame-example/albert-so/readmemd-edited-online-with-bitbucket-1519865641474 (pull request #2)
README.md edited online with Bitbucket
commit 73a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8
Author: Tom Brown <[email protected]m>
Date: Fri Apr 1 00:54:03 +0000
README.md edited online with Bitbucket

The git blame command works on individual files. The default execution of git blame outputs the blame information for the specified file.

The following output is a subset of the full blame output of the README file:

git blame

git blame README.md
83253a1b (marioswift 2019-02-28 13:37:02 -0800 1) # Git Blame example
83253a1b (marioswift 2019-02-28 13:37:02 -0800 2)
73a0b1c2 (Tom Brown 2019-04-01 00:54:03 +0000 3) Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
83253a1b (marioswift 2019-02-28 13:37:02 -0800 4)
83253a1b (marioswift 2019-02-28 13:37:02 -0800 5) There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.
83253a1b (marioswift 2019-02-28 13:37:02 -0800 6)
73a0b1c2 (Tom Brown 2019-04-01 00:54:03 +0000 7) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod TEMPOR incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laborum nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum

Common Options

You can also limit the output to specific lines using the -L option, as demonstrated in the table below.

git blame -L 1,3 README.mdLimits the output to the requested line range. Here we have limited the output to lines 1 through 3.
git blame -e README.mdDisplays the author’s email address instead of username.
git blame -w README.mdIgnores whitespace changes. If a previous author modified spacing (e.g., switching tabs to spaces), it can obscure the actual content changes in the output.
git blame -M README.mdThe -M option detects moved or copied lines within the same file. This will report the original author of the lines instead of the last author that moved or copied the lines.
git blame -C README.mdThe -C option detects lines that were moved or copied from other files. This will report the original author of the lines instead of the last author that moved or copied the lines.
git blame -p README.mdShows the full diff output for each line, making it easier to see exactly what was changed.

Git Blame vs Git Log

The git blame command shows the last author who changed a line, but sometimes you may need to see when a line was initially added. This can be difficult to do with git blame. For this purpose, a combination of -w, -C, and -M options can be used. But it will be easier to use git log.

Use git log with the -S option to display commits where a specific string was added or removed.

Let’s take the following example:

git log -S example

git log -S "CSS3D and WebGL renderers." --pretty=format:'%h %an %ad %s'
e339d3c85a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d John Carter Fri Jun 13 16:51:06 2015 +0200 reverted README.md to original content
509c2cc35a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d Max Fri Jul 8 13:56:14 2015 +0200 Updated README
cb20237cc1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6 Leo Sat Aug 31 00:22:36 2012 +0100 Removed DOMRenderer. Now with the CSS3DRenderer it has become irrelevant.

The output shows that the README.md file was added and modified 3 times by three authors. The --pretty=format:'...' option customizes the git log output to display specific fields like commit hash, author, date, and subject.

Practice

Practice

What are the functions and options of the 'git blame' command in Git?