34 Assigning Blame

Despite its combative name, git blame is a useful tool for determining what the original developer was thinking. Most bugs manifest themselves with an error at a specific point. You can use git blame to find out when the problem line was introduced into the repository and use that as a jumping-off point for further investigation.

git blame displays all or a portion of a file with annotations showing when the change was made, by who, and, more importantly, in what revision the change was made. Armed with that, you can inspect the log to determine what the original author intended.

git blame outputs the following information:

  • Short commit ID

  • Author’s name

  • Date and time of commit

  • Line number

By default, the entire file is displayed. You can limit the portion of the file displayed by using the -L parameter. It requires one parameter: a number or POSIX regular expression.

You can specify the point to stop, as well, by providing a second value as part of a comma-separated string. Make sure there’s no space between the start, the comma, and the second value. The second value can be another line number, a regular expression, or a number with a plus (+) or minus (-) before it.

The plus sign shows the start plus the number of lines; the minus sign adjusts the start to show the number of lines before the start. Remember that the plus and minus are zero-indexed. For example, -L 10,+10 shows lines 10 through 19, not lines 10 through 20.

Git can track content that moves around in a file or is copied from one file to another. You can use git blame to show content that has moved around by adding the -M parameter.

You can also track changes copied from another file by using the -C parameter. It checks the changes in the file against other changes in the repository to see whether it was copied from somewhere else.

What To Do...
  • Display file with entire line-by-line commit information.
     
    prompt>​ git blame some/file
  • Start the output of blame at line 10.
     
    prompt>​ git blame -L 10 some/file
  • Limit the output of blame to lines 10 through 20.
     
    prompt>​ git blame -L 10,20 some/file
     
    ...​ or ...
     
    prompt>​ git blame -L 10,+11 some/file
     
    ...​ or ...
     
    prompt>​ git blame -L 20,-11 some/file
  • Show ten lines of output from blame starting at a POSIX regular expression.
     
    prompt>​ git blame -L "/def to_s/",+10 some/file
  • Check the history to see whether the change was moved within the file, and display that information.
     
    prompt>​ git blame -M some/file
  • Check the history to see whether the change was copied from somewhere else or moved around within the file, and display that information.
     
    prompt>​ git blame -C some/file

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.239.103