NAME
git-blame - Show what revision and author last modified each line of a file
SYNOPSIS
- git blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental]
- [-L <range>] [-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>]
- [--ignore-rev <rev>] [--ignore-revs-file <file>]
- [--progress] [--abbrev=<n>] [<rev> | --contents <file> | --reverse <rev>..<rev>]
- [--] <file>
DESCRIPTION
Annotates each line in the given file with information from the revision whichlast modified the line. Optionally, start annotating from the given revision.
When specified one or more times, -L
restricts annotation to the requestedlines.
The origin of lines is automatically followed across whole-filerenames (currently there is no option to turn the rename-followingoff). To follow lines moved from one file to another, or to followlines that were copied and pasted from another file, etc., see the-C
and -M
options.
The report does not tell you anything about lines which have been deleted orreplaced; you need to use a tool such as git diff or the "pickaxe"interface briefly mentioned in the following paragraph.
Apart from supporting file annotation, Git also supports searching thedevelopment history for when a code snippet occurred in a change. This makes itpossible to track when a code snippet was added to a file, moved or copiedbetween files, and eventually deleted or replaced. It works by searching fora text string in the diff. A small example of the pickaxe interfacethat searches for blame_usage
:
- $ git log --pretty=oneline -S'blame_usage'
- 5040f17eba15504bad66b14a645bddd9b015ebb7 blame -S <ancestry-file>
- ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7 git-blame: Make the output
OPTIONS
- -b
Show blank SHA-1 for boundary commits. This can alsobe controlled via the
blame.blankboundary
config option.Do not treat root commits as boundaries. This can also becontrolled via the
blame.showRoot
config option.Include additional statistics at the end of blame output.
- -L :
- Annotate only the given line range. May be specified multiple times.Overlapping ranges are allowed.
-L
range, if any, otherwise from the start of file.If -L
range, if any, otherwisefrom the start of file. “^:git blame —reverse START
is taken as git blame—reverse START..HEAD
for convenience.- -p- —porcelain-Show in a format designed for machine consumption.- —line-porcelain-Show the porcelain format, but output commit information foreach line, not just the first time a commit is referenced.Implies —porcelain.- —incremental-Show the result incrementally in a format designed formachine consumption.- —encoding=none
makes blameoutput unconverted data. For more information see thediscussion about encoding in the git-log[1]manual page.- —contents -
to make the command read from the standard input).- —date —progress
together with —porcelain
or —incremental
.- -M[-M
, detect lines moved or copied from otherfiles that were modified in the same commit. This isuseful when you reorganize your program and move codearound across files. When this option is given twice,the command additionally looks for copies from otherfiles in the commit that creates the file. When thisoption is given three times, the command additionallylooks for copies from other files in any commit.-C
options given, the -C
willtake effect.- —ignore-rev blame.markIgnoredLines
config optionis set, then lines that were changed by an ignored commit and attributed toanother commit will be marked with a ?
in the blame output. If theblame.markUnblamableLines
config option is set, then those lines touchedby an ignored commit that we could not attribute to another revision aremarked with a *.- —ignore-revs-file file
, which must be in the same format as anfsck.skipList
. This option may be repeated, and these files will beprocessed after any files specified with the blame.ignoreRevsFile
configoption. An empty file name, ""
, will clear the list of revs frompreviously processed files.- -h-Show help message.- -c-Use the same output mode as git-annotate[1] (Default: off).- —score-debug-Include debugging information related to the movement oflines between files (see -C
) and lines moved within afile (see -M
). The first number listed is the score.This is the number of alphanumeric characters detectedas having been moved between or within files. This must be abovea certain threshold for git blame to consider those linesof code to have been moved.- -f- —show-name-Show the filename in the original commit. By defaultthe filename is shown if there is any line that came from afile with a different name, due to rename detection.- -n- —show-number-Show the line number in the original commit (Default: off).- -s-Suppress the author name and timestamp from the output.- -e- —show-email-Show the author email instead of author name (Default: off).This can also be controlled via the blame.showEmail
configoption.- -w-Ignore whitespace when comparing the parent’s version andthe child’s to find where the lines came from.- —abbrev=THE PORCELAIN FORMAT
In this format, each line is output after a header; theheader at the minimum has the first line which has:
40-byte SHA-1 of the commit the line is attributed to;
the line number of the line in the original file;
the line number of the line in the final file;
on a line that starts a group of lines from a differentcommit than the previous one, the number of lines in thisgroup. On subsequent lines this field is absent.
This header line is followed by the following informationat least once for each commit:
the author name ("author"), email ("author-mail"), time("author-time"), and time zone ("author-tz"); similarlyfor committer.
the filename in the commit that the line is attributed to.
the first line of the commit log message ("summary").
The contents of the actual line is output after the aboveheader, prefixed by a TAB. This is to allow adding moreheader elements later.
The porcelain format generally suppresses commit information that hasalready been seen. For example, two lines that are blamed to the samecommit will both be shown, but the details for that commit will be shownonly once. This is more efficient, but may require more state be kept bythe reader. The —line-porcelain
option can be used to output fullcommit information for each line, allowing simpler (but less efficient)usage like:
- # count the number of lines attributed to each author
- git blame --line-porcelain file |
- sed -n 's/^author //p' |
- sort | uniq -c | sort -rn
SPECIFYING RANGES
Unlike git blame and git annotate in older versions of git, the extentof the annotation can be limited to both line ranges and revisionranges. The -L
option, which limits annotation to a range of lines, may bespecified multiple times.
When you are interested in finding the origin forlines 40-60 for file foo
, you can use the -L
option like so(they mean the same thing — both ask for 21 lines starting atline 40):
- git blame -L 40,60 foo
- git blame -L 40,+21 foo
Also you can use a regular expression to specify the line range:
- git blame -L '/^sub hello {/,/^}$/' foo
which limits the annotation to the body of the hello
subroutine.
When you are not interested in changes older than versionv2.6.18, or changes older than 3 weeks, you can use revisionrange specifiers similar to git rev-list:
- git blame v2.6.18.. -- foo
- git blame --since=3.weeks -- foo
When revision range specifiers are used to limit the annotation,lines that have not changed since the range boundary (either thecommit v2.6.18 or the most recent commit that is more than 3weeks old in the above example) are blamed for that rangeboundary commit.
A particularly useful way is to see if an added file has linescreated by copy-and-paste from existing files. Sometimes thisindicates that the developer was being sloppy and did notrefactor the code properly. You can first find the commit thatintroduced the file with:
- git log --diff-filter=A --pretty=short -- foo
and then annotate the change between the commit and itsparents, using commit^!
notation:
- git blame -C -C -f $commit^! -- foo
INCREMENTAL OUTPUT
When called with —incremental
option, the command outputs theresult as it is built. The output generally will talk aboutlines touched by more recent commits first (i.e. the lines willbe annotated out of order) and is meant to be used byinteractive viewers.
The output format is similar to the Porcelain format, but itdoes not contain the actual lines from the file that is beingannotated.
- Each blame entry always starts with a line of:
- <40-byte hex sha1> <sourceline> <resultline> <num_lines>
Line numbers count from 1.
The first time that a commit shows up in the stream, it has variousother information about it printed out with a one-word tag at thebeginning of each line describing the extra commit information (author,email, committer, dates, summary, etc.).
Unlike the Porcelain format, the filename information is alwaysgiven and terminates the entry:
- "filename" <whitespace-quoted-filename-goes-here>
and thus it is really quite easy to parse for some line- and word-orientedparser (which should be quite natural for most scripting languages).
NoteFor people who do parsing: to make it more robust, just ignore anylines between the first and last one ("
MAPPING AUTHORS
If the file .mailmap
exists at the toplevel of the repository, or atthe location pointed to by the mailmap.file or mailmap.blobconfiguration options, itis used to map author and committer names and email addresses tocanonical real names and email addresses.
In the simple form, each line in the file consists of the canonicalreal name of an author, whitespace, and an email address used in thecommit (enclosed by < and >) to map to the name. For example:
- Proper Name <commit@email.xx>
The more complex forms are:
- <proper@email.xx> <commit@email.xx>
which allows mailmap to replace only the email part of a commit, and:
- Proper Name <proper@email.xx> <commit@email.xx>
which allows mailmap to replace both the name and the email of acommit matching the specified commit email address, and:
- Proper Name <proper@email.xx> Commit Name <commit@email.xx>
which allows mailmap to replace both the name and the email of acommit matching both the specified commit name and email address.
Example 1: Your history contains commits by two authors, Janeand Joe, whose names appear in the repository under several forms:
- Joe Developer <joe@example.com>
- Joe R. Developer <joe@example.com>
- Jane Doe <jane@example.com>
- Jane Doe <jane@laptop.(none)>
- Jane D. <jane@desktop.(none)>
Now suppose that Joe wants his middle name initial used, and Janeprefers her family name fully spelled out. A proper .mailmap
filewould look like:
- Jane Doe <jane@desktop.(none)>
- Joe R. Developer <joe@example.com>
Note how there is no need for an entry for <jane@laptop.(none)>
, because thereal name of that author is already correct.
Example 2: Your repository contains commits from the followingauthors:
- nick1 <bugs@company.xx>
- nick2 <bugs@company.xx>
- nick2 <nick2@company.xx>
- santa <me@company.xx>
- claus <me@company.xx>
- CTO <cto@coompany.xx>
Then you might want a .mailmap
file that looks like:
- <cto@company.xx> <cto@coompany.xx>
- Some Dude <some@dude.xx> nick1 <bugs@company.xx>
- Other Author <other@author.xx> nick2 <bugs@company.xx>
- Other Author <other@author.xx> <nick2@company.xx>
- Santa Claus <santa.claus@northpole.xx> <me@company.xx>
Use hash # for comments that are either on their own line, or afterthe email address.
SEE ALSO
GIT
Part of the git[1] suite