Documentation / git-blame.txton commit gitweb: Convert project name to UTF-8 (0417941)
   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] [-S <revs-file>]
  12            [-M] [-C] [-C] [--since=<date>] [<rev>] [--] <file>
  13
  14DESCRIPTION
  15-----------
  16
  17Annotates each line in the given file with information from the revision which
  18last modified the line. Optionally, start annotating from the given revision.
  19
  20Also it can limit the range of lines annotated.
  21
  22This report doesn't tell you anything about lines which have been deleted or
  23replaced; you need to use a tool such as gitlink:git-diff[1] or the "pickaxe"
  24interface briefly mentioned in the following paragraph.
  25
  26Apart from supporting file annotation, git also supports searching the
  27development history for when a code snippet occurred in a change. This makes it
  28possible to track when a code snippet was added to a file, moved or copied
  29between files, and eventually deleted or replaced. It works by searching for
  30a text string in the diff. A small example:
  31
  32-----------------------------------------------------------------------------
  33$ git log --pretty=oneline -S'blame_usage'
  345040f17eba15504bad66b14a645bddd9b015ebb7 blame -S <ancestry-file>
  35ea4c7f9bf69e781dd0cd88d2bccb2bf5cc15c9a7 git-blame: Make the output
  36-----------------------------------------------------------------------------
  37
  38OPTIONS
  39-------
  40-c, --compatibility::
  41        Use the same output mode as gitlink:git-annotate[1] (Default: off).
  42
  43-L n,m::
  44        Annotate only the specified line range (lines count from 1).
  45
  46-l, --long::
  47        Show long rev (Default: off).
  48
  49-t, --time::
  50        Show raw timestamp (Default: off).
  51
  52-S, --rev-file <revs-file>::
  53        Use revs from revs-file instead of calling gitlink:git-rev-list[1].
  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
  63-p, --porcelain::
  64        Show in a format designed for machine consumption.
  65
  66--incremental::
  67        Show the result incrementally in a format designed for
  68        machine consumption.
  69
  70-M::
  71        Detect moving lines in the file as well.  When a commit
  72        moves a block of lines in a file (e.g. the original file
  73        has A and then B, and the commit changes it to B and
  74        then A), traditional 'blame' algorithm typically blames
  75        the lines that were moved up (i.e. B) to the parent and
  76        assigns blame to the lines that were moved down (i.e. A)
  77        to the child commit.  With this option, both groups of
  78        lines are blamed on the parent.
  79
  80-C::
  81        In addition to `-M`, detect lines copied from other
  82        files that were modified in the same commit.  This is
  83        useful when you reorganize your program and move code
  84        around across files.  When this option is given twice,
  85        the command looks for copies from all other files in the
  86        parent for the commit that creates the file in addition.
  87
  88-h, --help::
  89        Show help message.
  90
  91
  92THE PORCELAIN FORMAT
  93--------------------
  94
  95In this format, each line is output after a header; the
  96header at the minimum has the first line which has:
  97
  98- 40-byte SHA-1 of the commit the line is attributed to;
  99- the line number of the line in the original file;
 100- the line number of the line in the final file;
 101- on a line that starts a group of line from a different
 102  commit than the previous one, the number of lines in this
 103  group.  On subsequent lines this field is absent.
 104
 105This header line is followed by the following information
 106at least once for each commit:
 107
 108- author name ("author"), email ("author-mail"), time
 109  ("author-time"), and timezone ("author-tz"); similarly
 110  for committer.
 111- filename in the commit the line is attributed to.
 112- the first line of the commit log message ("summary").
 113
 114The contents of the actual line is output after the above
 115header, prefixed by a TAB. This is to allow adding more
 116header elements later.
 117
 118
 119SPECIFYING RANGES
 120-----------------
 121
 122Unlike `git-blame` and `git-annotate` in older git, the extent
 123of annotation can be limited to both line ranges and revision
 124ranges.  When you are interested in finding the origin for
 125ll. 40-60 for file `foo`, you can use `-L` option like these
 126(they mean the same thing -- both ask for 21 lines starting at
 127line 40):
 128
 129        git blame -L 40,60 foo
 130        git blame -L 40,+21 foo
 131
 132Also you can use regular expression to specify the line range.
 133
 134        git blame -L '/^sub hello {/,/^}$/' foo
 135
 136would limit the annotation to the body of `hello` subroutine.
 137
 138When you are not interested in changes older than the version
 139v2.6.18, or changes older than 3 weeks, you can use revision
 140range specifiers  similar to `git-rev-list`:
 141
 142        git blame v2.6.18.. -- foo
 143        git blame --since=3.weeks -- foo
 144
 145When revision range specifiers are used to limit the annotation,
 146lines that have not changed since the range boundary (either the
 147commit v2.6.18 or the most recent commit that is more than 3
 148weeks old in the above example) are blamed for that range
 149boundary commit.
 150
 151A particularly useful way is to see if an added file have lines
 152created by copy-and-paste from existing files.  Sometimes this
 153indicates that the developer was being sloppy and did not
 154refactor the code properly.  You can first find the commit that
 155introduced the file with:
 156
 157        git log --diff-filter=A --pretty=short -- foo
 158
 159and then annotate the change between the commit and its
 160parents, using `commit{caret}!` notation:
 161
 162        git blame -C -C -f $commit^! -- foo
 163
 164
 165INCREMENTAL OUTPUT
 166------------------
 167
 168When called with `--incremental` option, the command outputs the
 169result as it is built.  The output generally will talk about
 170lines touched by more recent commits first (i.e. the lines will
 171be annotated out of order) and is meant to be used by
 172interactive viewers.
 173
 174The output format is similar to the Porcelain format, but it
 175does not contain the actual lines from the file that is being
 176annotated.
 177
 178. Each blame entry always starts with a line of:
 179
 180        <40-byte hex sha1> <sourceline> <resultline> <num_lines>
 181+
 182Line numbers count from 1.
 183
 184. The first time that commit shows up in the stream, it has various
 185  other information about it printed out with a one-word tag at the
 186  beginning of each line about that "extended commit info" (author,
 187  email, committer, dates, summary etc).
 188
 189. Unlike Porcelain format, the filename information is always
 190  given and terminates the entry:
 191
 192        "filename" <whitespace-quoted-filename-goes-here>
 193+
 194and thus it's really quite easy to parse for some line- and word-oriented
 195parser (which should be quite natural for most scripting languages).
 196+
 197[NOTE]
 198For people who do parsing: to make it more robust, just ignore any
 199lines in between the first and last one ("<sha1>" and "filename" lines)
 200where you don't recognize the tag-words (or care about that particular
 201one) at the beginning of the "extended information" lines. That way, if
 202there is ever added information (like the commit encoding or extended
 203commit commentary), a blame viewer won't ever care.
 204
 205
 206SEE ALSO
 207--------
 208gitlink:git-annotate[1]
 209
 210AUTHOR
 211------
 212Written by Junio C Hamano <junkio@cox.net>
 213
 214GIT
 215---
 216Part of the gitlink:git[7] suite