git blame

Git blame

Description

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.

How it works

How it works

So as to clearly show how git blame works, let’s consider the example where we have 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 has the following look:

git log
commit 745nurcid82e4e5f3734c219d5a742b1c259926b2
Author: Bob Smith <bobsmith@3wdocs.com>
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 <bobsmith@3wdocs.com>
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]>
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 73nov64c885fe33d1182f2112885c2a64a4206ec
Author: Tom Brown <[email protected]>
Date: Fri Apr 1 00:54:03 2019 +0000
README.md edited online with Bitbucket

The git blame works on individual files. The default execution of git blame outputs the commands help menu.

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

git blame README.md
83253ua2 (marioswift 2019-02-28 13:37:02 -0800 1) # Git Blame example
83253ua2 (marioswift 2019-02-28 13:37:02 -0800 2)
73nov64c (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.
83253ua2 (marioswift 2019-02-28 13:37:02 -0800 4)
83253ua2 (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.
83253ua2(marioswift 2019-02-28 13:37:02 -0800 6)
73nov64c (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 laboris 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

Common Options

类目类目
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 has modified the spacing of a file by switching from tabs to spaces or adding new lines this, unfortunately, obscures the output of git blame by showing these changes.
git blame -M README.mdThe -M option detects moved or copied lines within in 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 vs Git Log

Git Blame vs Git Log

The git blame 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 all initial commits with a particular added or changed code.

Let’s take the following example:

git log -S"CSS3D and WebGL renderers." --pretty=format:'%h %an %ad %s'
e339d3c85 John Carter Fri Jun 13 16:51:06 2015 +0200 reverted README.md to original content
509c2cc35 Max Fri Jul 8 13:56:14 2015 +0200 Updated README
cb20237cc 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 converts the initial git log output format into one that corresponds the format of git log.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部