NAME

git-blame - Show what revision and author last modified each line of a file

SYNOPSIS

  1. git blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental]
  2. [-L <range>] [-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>]
  3. [--ignore-rev <rev>] [--ignore-revs-file <file>]
  4. [--progress] [--abbrev=<n>] [<rev> | --contents <file> | --reverse <rev>..<rev>]
  5. [--] <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:

  1. $ git log --pretty=oneline -S'blame_usage'
  2. 5040f17eba15504bad66b14a645bddd9b015ebb7 blame -S <ancestry-file>
  3. 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.

  • —root

  • Do not treat root commits as boundaries. This can also becontrolled via the blame.showRoot config option.

  • —show-stats

  • Include additional statistics at the end of blame output.

  • -L ,

  • -L :
  • Annotate only the given line range. May be specified multiple times.Overlapping ranges are allowed.
and are optional. “-L ” or “-L ,” spans from to end of file. “-L ,” spans from start of file to . and can take one of these forms: -numberIf or is a number, it specifies anabsolute line number (lines count from 1). -/regex/This form will use the first line matching the givenPOSIX regex. If is a regex, it will search from the end ofthe previous -L range, if any, otherwise from the start of file.If is “^/regex/”, it will search from the start of file.If is a regex, it will searchstarting at the line given by . -+offset or -offsetThis is only valid for and will specify a numberof lines before or after the line given by .If “:” is given in place of and , it is aregular expression that denotes the range from the first funcname linethat matches , up to the next funcname line. “:”searches from the end of the previous -L range, if any, otherwisefrom the start of file. “^:” searches from the start offile.- -l-Show long rev (Default: off).- -t-Show raw timestamp (Default: off).- -S -Use revisions from revs-file instead of calling git-rev-list[1].- —reverse ..-Walk history forward instead of backward. Instead of showingthe revision in which a line appeared, this shows the lastrevision in which a line has existed. This requires a range ofrevision like START..END where the path to blame exists inSTART. 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=-Specifies the encoding used to output author namesand commit summaries. Setting it to none makes blameoutput unconverted data. For more information see thediscussion about encoding in the git-log[1]manual page.- —contents -When is not specified, the command annotates thechanges starting backwards from the working tree copy.This flag makes the command pretend as if the workingtree copy has the contents of the named file (specify- to make the command read from the standard input).- —date -Specifies the format used to output dates. If —date is notprovided, the value of the blame.date config variable isused. If the blame.date config variable is also not set, theiso format is used. For supported values, see the discussionof the —date option at git-log[1].- —[no-]progress-Progress status is reported on the standard error streamby default when it is attached to a terminal. This flagenables progress reporting even if not attached to aterminal. Can’t use —progress together with —porcelainor —incremental.- -M[]-Detect moved or copied lines within a file. When a commitmoves or copies a block of lines (e.g. the original filehas A and then B, and the commit changes it to B and thenA), the traditional blame algorithm notices only half ofthe movement and typically blames the lines that were movedup (i.e. B) to the parent and assigns blame to the lines thatwere moved down (i.e. A) to the child commit. With thisoption, both groups of lines are blamed on the parent byrunning extra passes of inspection. is optional but it is the lower bound on the number ofalphanumeric characters that Git must detect as moving/copyingwithin a file for it to associate those lines with the parentcommit. The default value is 20.- -C[]-In addition to -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. is optional but it is the lower bound on the number ofalphanumeric characters that Git must detect as moving/copyingbetween files for it to associate those lines with the parentcommit. And the default value is 40. If there are more than one-C options given, the argument of the last -C willtake effect.- —ignore-rev -Ignore changes made by the revision when assigning blame, as if thechange never happened. Lines that were changed or added by an ignoredcommit will be blamed on the previous commit that changed that line ornearby lines. This option may be specified multiple times to ignoremore than one revision. If the 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 -Ignore revisions listed in 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=-Instead of using the default 7+1 hexadecimal digits as theabbreviated object name, use +1 digits. Note that 1 columnis used for a caret to mark the boundary commit.

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:

  1. # count the number of lines attributed to each author
  2. git blame --line-porcelain file |
  3. sed -n 's/^author //p' |
  4. 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):

  1. git blame -L 40,60 foo
  2. git blame -L 40,+21 foo

Also you can use a regular expression to specify the line range:

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

  1. git blame v2.6.18.. -- foo
  2. 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:

  1. git log --diff-filter=A --pretty=short -- foo

and then annotate the change between the commit and itsparents, using commit^! notation:

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

  1. "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 ("" and "filename" lines)where you do not recognize the tag words (or care about that particularone) at the beginning of the "extended information" lines. That way, ifthere is ever added information (like the commit encoding or extendedcommit commentary), a blame viewer will not care.

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:

  1. Proper Name <commit@email.xx>

The more complex forms are:

  1. <proper@email.xx> <commit@email.xx>

which allows mailmap to replace only the email part of a commit, and:

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

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

  1. Joe Developer <joe@example.com>
  2. Joe R. Developer <joe@example.com>
  3. Jane Doe <jane@example.com>
  4. Jane Doe <jane@laptop.(none)>
  5. 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:

  1. Jane Doe <jane@desktop.(none)>
  2. 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:

  1. nick1 <bugs@company.xx>
  2. nick2 <bugs@company.xx>
  3. nick2 <nick2@company.xx>
  4. santa <me@company.xx>
  5. claus <me@company.xx>
  6. CTO <cto@coompany.xx>

Then you might want a .mailmap file that looks like:

  1. <cto@company.xx> <cto@coompany.xx>
  2. Some Dude <some@dude.xx> nick1 <bugs@company.xx>
  3. Other Author <other@author.xx> nick2 <bugs@company.xx>
  4. Other Author <other@author.xx> <nick2@company.xx>
  5. 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-annotate[1]

GIT

Part of the git[1] suite