<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-blame(1)">
<articleinfo>
    <title>git-blame(1)</title>
        <indexterm>
                <primary>git-blame(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-blame - Show what revision and author last modified each line of a file</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git blame</emphasis> [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [--incremental] [-L n,m]
            [-S &lt;revs-file&gt;] [-M] [-C] [-C] [--since=&lt;date&gt;]
            [&lt;rev&gt; | --contents &lt;file&gt;] [--] &lt;file&gt;</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Annotates each line in the given file with information from the revision which
last modified the line. Optionally, start annotating from the given revision.</simpara>
<simpara>Also it can limit the range of lines annotated.</simpara>
<simpara>This report doesn&#8217;t tell you anything about lines which have been deleted or
replaced; you need to use a tool such as <emphasis>git-diff</emphasis> or the "pickaxe"
interface briefly mentioned in the following paragraph.</simpara>
<simpara>Apart from supporting file annotation, git also supports searching the
development history for when a code snippet occurred in a change. This makes it
possible to track when a code snippet was added to a file, moved or copied
between files, and eventually deleted or replaced. It works by searching for
a text string in the diff. A small example:</simpara>
<literallayout>$ git log --pretty=oneline -S'blame_usage'
5040f17eba15504bad66b14a645bddd9b015ebb7 blame -S &lt;ancestry-file&gt;
ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7 git-blame: Make the output</literallayout>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
-b
</term>
<listitem>
<simpara>
        Show blank SHA-1 for boundary commits.  This can also
        be controlled via the <literal>blame.blankboundary</literal> config option.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--root
</term>
<listitem>
<simpara>
        Do not treat root commits as boundaries.  This can also be
        controlled via the <literal>blame.showroot</literal> config option.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--show-stats
</term>
<listitem>
<simpara>
        Include additional statistics at the end of blame output.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-L &lt;start&gt;,&lt;end&gt;
</term>
<listitem>
<simpara>
        Annotate only the given line range.  &lt;start&gt; and &lt;end&gt; can take
        one of these forms:
</simpara>
<itemizedlist>
<listitem>
<simpara>
number
</simpara>
<simpara>If &lt;start&gt; or &lt;end&gt; is a number, it specifies an
absolute line number (lines count from 1).</simpara>
</listitem>
<listitem>
<simpara>
/regex/
</simpara>
<simpara>This form will use the first line matching the given
POSIX regex.  If &lt;end&gt; is a regex, it will search
starting at the line given by &lt;start&gt;.</simpara>
</listitem>
<listitem>
<simpara>
+offset or -offset
</simpara>
<simpara>This is only valid for &lt;end&gt; and will specify a number
of lines before or after the line given by &lt;start&gt;.</simpara>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
-l
</term>
<listitem>
<simpara>
        Show long rev (Default: off).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-t
</term>
<listitem>
<simpara>
        Show raw timestamp (Default: off).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-S &lt;revs-file&gt;
</term>
<listitem>
<simpara>
        Use revs from revs-file instead of calling <xref linkend="git-rev-list(1)"/>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-p
</term>
<term>
--porcelain
</term>
<listitem>
<simpara>
        Show in a format designed for machine consumption.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--incremental
</term>
<listitem>
<simpara>
        Show the result incrementally in a format designed for
        machine consumption.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--encoding=&lt;encoding&gt;
</term>
<listitem>
<simpara>
        Specifies the encoding used to output author names
        and commit summaries. Setting it to <literal>none</literal> makes blame
        output unconverted data. For more information see the
        discussion about encoding in the <xref linkend="git-log(1)"/>
        manual page.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--contents &lt;file&gt;
</term>
<listitem>
<simpara>
        When &lt;rev&gt; is not specified, the command annotates the
        changes starting backwards from the working tree copy.
        This flag makes the command pretend as if the working
        tree copy has the contents of the named file (specify
        <literal>-</literal> to make the command read from the standard input).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-M|&lt;num&gt;|
</term>
<listitem>
<simpara>
        Detect moving lines in the file as well.  When a commit
        moves a block of lines in a file (e.g. the original file
        has A and then B, and the commit changes it to B and
        then A), traditional <emphasis>blame</emphasis> algorithm typically blames
        the lines that were moved up (i.e. B) to the parent and
        assigns blame to the lines that were moved down (i.e. A)
        to the child commit.  With this option, both groups of lines
        are blamed on the parent.
</simpara>
<simpara>&lt;num&gt; is optional but it is the lower bound on the number of
alphanumeric characters that git must detect as moving
within a file for it to associate those lines with the parent
commit.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-C|&lt;num&gt;|
</term>
<listitem>
<simpara>
        In addition to <literal>-M</literal>, detect lines copied from other
        files that were modified in the same commit.  This is
        useful when you reorganize your program and move code
        around across files.  When this option is given twice,
        the command looks for copies from all other files in the
        parent for the commit that creates the file in addition.
</simpara>
<simpara>&lt;num&gt; is optional but it is the lower bound on the number of
alphanumeric characters that git must detect as moving
between files for it to associate those lines with the parent
commit.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-h
</term>
<term>
--help
</term>
<listitem>
<simpara>
        Show help message.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-c
</term>
<listitem>
<simpara>
        Use the same output mode as <xref linkend="git-annotate(1)"/> (Default: off).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--score-debug
</term>
<listitem>
<simpara>
        Include debugging information related to the movement of
        lines between files (see <literal>-C</literal>) and lines moved within a
        file (see <literal>-M</literal>).  The first number listed is the score.
        This is the number of alphanumeric characters detected
        to be moved between or within files.  This must be above
        a certain threshold for <emphasis>git-blame</emphasis> to consider those lines
        of code to have been moved.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-f
</term>
<term>
--show-name
</term>
<listitem>
<simpara>
        Show filename in the original commit.  By default
        filename is shown if there is any line that came from a
        file with different name, due to rename detection.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-n
</term>
<term>
--show-number
</term>
<listitem>
<simpara>
        Show line number in the original commit (Default: off).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-s
</term>
<listitem>
<simpara>
        Suppress author name and timestamp from the output.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-w
</term>
<listitem>
<simpara>
        Ignore whitespace when comparing parent&#8217;s version and
        child&#8217;s to find where the lines came from.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_the_porcelain_format">
<title>THE PORCELAIN FORMAT</title>
<simpara>In this format, each line is output after a header; the
header at the minimum has the first line which has:</simpara>
<itemizedlist>
<listitem>
<simpara>
40-byte SHA-1 of the commit the line is attributed to;
</simpara>
</listitem>
<listitem>
<simpara>
the line number of the line in the original file;
</simpara>
</listitem>
<listitem>
<simpara>
the line number of the line in the final file;
</simpara>
</listitem>
<listitem>
<simpara>
on a line that starts a group of line from a different
  commit than the previous one, the number of lines in this
  group.  On subsequent lines this field is absent.
</simpara>
</listitem>
</itemizedlist>
<simpara>This header line is followed by the following information
at least once for each commit:</simpara>
<itemizedlist>
<listitem>
<simpara>
author name ("author"), email ("author-mail"), time
  ("author-time"), and timezone ("author-tz"); similarly
  for committer.
</simpara>
</listitem>
<listitem>
<simpara>
filename in the commit the line is attributed to.
</simpara>
</listitem>
<listitem>
<simpara>
the first line of the commit log message ("summary").
</simpara>
</listitem>
</itemizedlist>
<simpara>The contents of the actual line is output after the above
header, prefixed by a TAB. This is to allow adding more
header elements later.</simpara>
</simplesect>
<simplesect id="_specifying_ranges">
<title>SPECIFYING RANGES</title>
<simpara>Unlike <emphasis>git-blame</emphasis> and <emphasis>git-annotate</emphasis> in older git, the extent
of annotation can be limited to both line ranges and revision
ranges.  When you are interested in finding the origin for
ll. 40-60 for file <literal>foo</literal>, you can use <literal>-L</literal> option like these
(they mean the same thing&#8201;&#8212;&#8201;both ask for 21 lines starting at
line 40):</simpara>
<literallayout class="monospaced">git blame -L 40,60 foo
git blame -L 40,+21 foo</literallayout>
<simpara>Also you can use regular expression to specify the line range.</simpara>
<literallayout class="monospaced">git blame -L '/^sub hello {/,/^}$/' foo</literallayout>
<simpara>would limit the annotation to the body of <literal>hello</literal> subroutine.</simpara>
<simpara>When you are not interested in changes older than the version
v2.6.18, or changes older than 3 weeks, you can use revision
range specifiers  similar to <emphasis>git-rev-list</emphasis>:</simpara>
<literallayout class="monospaced">git blame v2.6.18.. -- foo
git blame --since=3.weeks -- foo</literallayout>
<simpara>When revision range specifiers are used to limit the annotation,
lines that have not changed since the range boundary (either the
commit v2.6.18 or the most recent commit that is more than 3
weeks old in the above example) are blamed for that range
boundary commit.</simpara>
<simpara>A particularly useful way is to see if an added file have lines
created by copy-and-paste from existing files.  Sometimes this
indicates that the developer was being sloppy and did not
refactor the code properly.  You can first find the commit that
introduced the file with:</simpara>
<literallayout class="monospaced">git log --diff-filter=A --pretty=short -- foo</literallayout>
<simpara>and then annotate the change between the commit and its
parents, using <literal>commit&#94;!</literal> notation:</simpara>
<literallayout class="monospaced">git blame -C -C -f $commit^! -- foo</literallayout>
</simplesect>
<simplesect id="_incremental_output">
<title>INCREMENTAL OUTPUT</title>
<simpara>When called with <literal>--incremental</literal> option, the command outputs the
result as it is built.  The output generally will talk about
lines touched by more recent commits first (i.e. the lines will
be annotated out of order) and is meant to be used by
interactive viewers.</simpara>
<simpara>The output format is similar to the Porcelain format, but it
does not contain the actual lines from the file that is being
annotated.</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
Each blame entry always starts with a line of:
</simpara>
<literallayout class="monospaced">&lt;40-byte hex sha1&gt; &lt;sourceline&gt; &lt;resultline&gt; &lt;num_lines&gt;</literallayout>
<simpara>Line numbers count from 1.</simpara>
</listitem>
<listitem>
<simpara>
The first time that commit shows up in the stream, it has various
  other information about it printed out with a one-word tag at the
  beginning of each line about that "extended commit info" (author,
  email, committer, dates, summary etc).
</simpara>
</listitem>
<listitem>
<simpara>
Unlike Porcelain format, the filename information is always
  given and terminates the entry:
</simpara>
<literallayout class="monospaced">"filename" &lt;whitespace-quoted-filename-goes-here&gt;</literallayout>
<simpara>and thus it&#8217;s really quite easy to parse for some line- and word-oriented
parser (which should be quite natural for most scripting languages).</simpara>
<note><simpara>For people who do parsing: to make it more robust, just ignore any
lines in between the first and last one ("&lt;sha1&gt;" and "filename" lines)
where you don&#8217;t recognize the tag-words (or care about that particular
one) at the beginning of the "extended information" lines. That way, if
there is ever added information (like the commit encoding or extended
commit commentary), a blame viewer won&#8217;t ever care.</simpara></note>
</listitem>
</orderedlist>
</simplesect>
<simplesect id="_see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="git-annotate(1)"/></simpara>
</simplesect>
<simplesect id="_author">
<title>AUTHOR</title>
<simpara>Written by Junio C Hamano &lt;<ulink url="mailto:gitster@pobox.com">gitster@pobox.com</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
