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... |
| prompt> git blame some/file |
| prompt> git blame -L 10 some/file |
| 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 |
| prompt> git blame -L "/def to_s/",+10 some/file |
| prompt> git blame -M some/file |
| prompt> git blame -C some/file |
Task 30, Viewing the Log
Task 32, Comparing Differences
3.143.239.103