Documentation / git-blame.txton commit Merge branch 'jc/the-index' (da94faf)
   1git-blame(1)
   2============
   3
   4NAME
   5----
   6git-blame - Show what revision and author last modified each line of a file
   7
   8SYNOPSIS
   9--------
  10[verse]
  11'git-blame' [-c] [-l] [-t] [-f] [-n] [-p] [--incremental] [-L n,m]
  12            [-S <revs-file>] [-M] [-C] [-C] [--since=<date>]
  13            [<rev> | --contents <file>] [--] <file>
  14
  15DESCRIPTION
  16-----------
  17
  18Annotates each line in the given file with information from the revision which
  19last modified the line. Optionally, start annotating from the given revision.
  20
  21Also it can limit the range of lines annotated.
  22
  23This report doesn't tell you anything about lines which have been deleted or
  24replaced; you need to use a tool such as gitlink:git-diff[1] or the "pickaxe"
  25interface briefly mentioned in the following paragraph.
  26
  27Apart from supporting file annotation, git also supports searching the
  28development history for when a code snippet occurred in a change. This makes it
  29possible to track when a code snippet was added to a file, moved or copied
  30between files, and eventually deleted or replaced. It works by searching for
  31a text string in the diff. A small example:
  32
  33-----------------------------------------------------------------------------
  34$ git log --pretty=oneline -S'blame_usage'
  355040f17eba15504bad66b14a645bddd9b015ebb7 blame -S <ancestry-file>
  36ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7 git-blame: Make the output
  37-----------------------------------------------------------------------------
  38
  39OPTIONS
  40-------
  41include::blame-options.txt[]
  42
  43-c::
  44        Use the same output mode as gitlink:git-annotate[1] (Default: off).
  45
  46--score-debug::
  47        Include debugging information related to the movement of
  48        lines between files (see `-C`) and lines moved within a
  49        file (see `-M`).  The first number listed is the score.
  50        This is the number of alphanumeric characters detected
  51        to be moved between or within files.  This must be above
  52        a certain threshold for git-blame to consider those lines
  53        of code to have been moved.
  54
  55-f, --show-name::
  56        Show filename in the original commit.  By default
  57        filename is shown if there is any line that came from a
  58        file with different name, due to rename detection.
  59
  60-n, --show-number::
  61        Show line number in the original commit (Default: off).
  62
  63THE PORCELAIN FORMAT
  64--------------------
  65
  66In this format, each line is output after a header; the
  67header at the minimum has the first line which has:
  68
  69- 40-byte SHA-1 of the commit the line is attributed to;
  70- the line number of the line in the original file;
  71- the line number of the line in the final file;
  72- on a line that starts a group of line from a different
  73  commit than the previous one, the number of lines in this
  74  group.  On subsequent lines this field is absent.
  75
  76This header line is followed by the following information
  77at least once for each commit:
  78
  79- author name ("author"), email ("author-mail"), time
  80  ("author-time"), and timezone ("author-tz"); similarly
  81  for committer.
  82- filename in the commit the line is attributed to.
  83- the first line of the commit log message ("summary").
  84
  85The contents of the actual line is output after the above
  86header, prefixed by a TAB. This is to allow adding more
  87header elements later.
  88
  89
  90SPECIFYING RANGES
  91-----------------
  92
  93Unlike `git-blame` and `git-annotate` in older git, the extent
  94of annotation can be limited to both line ranges and revision
  95ranges.  When you are interested in finding the origin for
  96ll. 40-60 for file `foo`, you can use `-L` option like these
  97(they mean the same thing -- both ask for 21 lines starting at
  98line 40):
  99
 100        git blame -L 40,60 foo
 101        git blame -L 40,+21 foo
 102
 103Also you can use regular expression to specify the line range.
 104
 105        git blame -L '/^sub hello {/,/^}$/' foo
 106
 107would limit the annotation to the body of `hello` subroutine.
 108
 109When you are not interested in changes older than the version
 110v2.6.18, or changes older than 3 weeks, you can use revision
 111range specifiers  similar to `git-rev-list`:
 112
 113        git blame v2.6.18.. -- foo
 114        git blame --since=3.weeks -- foo
 115
 116When revision range specifiers are used to limit the annotation,
 117lines that have not changed since the range boundary (either the
 118commit v2.6.18 or the most recent commit that is more than 3
 119weeks old in the above example) are blamed for that range
 120boundary commit.
 121
 122A particularly useful way is to see if an added file have lines
 123created by copy-and-paste from existing files.  Sometimes this
 124indicates that the developer was being sloppy and did not
 125refactor the code properly.  You can first find the commit that
 126introduced the file with:
 127
 128        git log --diff-filter=A --pretty=short -- foo
 129
 130and then annotate the change between the commit and its
 131parents, using `commit{caret}!` notation:
 132
 133        git blame -C -C -f $commit^! -- foo
 134
 135
 136INCREMENTAL OUTPUT
 137------------------
 138
 139When called with `--incremental` option, the command outputs the
 140result as it is built.  The output generally will talk about
 141lines touched by more recent commits first (i.e. the lines will
 142be annotated out of order) and is meant to be used by
 143interactive viewers.
 144
 145The output format is similar to the Porcelain format, but it
 146does not contain the actual lines from the file that is being
 147annotated.
 148
 149. Each blame entry always starts with a line of:
 150
 151        <40-byte hex sha1> <sourceline> <resultline> <num_lines>
 152+
 153Line numbers count from 1.
 154
 155. The first time that commit shows up in the stream, it has various
 156  other information about it printed out with a one-word tag at the
 157  beginning of each line about that "extended commit info" (author,
 158  email, committer, dates, summary etc).
 159
 160. Unlike Porcelain format, the filename information is always
 161  given and terminates the entry:
 162
 163        "filename" <whitespace-quoted-filename-goes-here>
 164+
 165and thus it's really quite easy to parse for some line- and word-oriented
 166parser (which should be quite natural for most scripting languages).
 167+
 168[NOTE]
 169For people who do parsing: to make it more robust, just ignore any
 170lines in between the first and last one ("<sha1>" and "filename" lines)
 171where you don't recognize the tag-words (or care about that particular
 172one) at the beginning of the "extended information" lines. That way, if
 173there is ever added information (like the commit encoding or extended
 174commit commentary), a blame viewer won't ever care.
 175
 176
 177SEE ALSO
 178--------
 179gitlink:git-annotate[1]
 180
 181AUTHOR
 182------
 183Written by Junio C Hamano <junkio@cox.net>
 184
 185GIT
 186---
 187Part of the gitlink:git[7] suite