git-blame.txt 8.0 KB

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