The git blame
command is used to examine the content of any file line by line. It helps you determine who made changes to a file and when those changes were made. This command is particularly useful for tracking down the origin of a specific change or understanding the history of a file.
Initial Example
$ git blame <your_file_name>
BLAME Options
Option | Description |
---|---|
-b | Show blank SHA-1 for boundary commits. Controlled by blame.blankBoundary config option. |
--root | Do not treat root commits as boundaries. Controlled by blame.showRoot config option. |
--show-stats | Include additional statistics at the end of blame output. |
-L <start>,<end> -L :<funcname> | Annotate only the specified line range or function name regex. Can be specified multiple times. |
-l | Show long revision hashes. |
-t | Show raw timestamp. |
-S <revs-file> | Use revisions from the specified file instead of calling git-rev-list . |
--reverse <rev>..<rev> | Walk history forward instead of backward. Shows the last revision in which a line existed. Requires a range of revisions. |
--first-parent | Follow only the first parent commit upon seeing a merge commit. |
-p , --porcelain | Show in a format designed for machine consumption. |
--line-porcelain | Show the porcelain format with commit information for each line. Implies --porcelain . |
--incremental | Show the result incrementally in a format designed for machine consumption. |
--encoding=<encoding> | Specifies the encoding used for outputting author names and commit summaries. |
--contents <file> | Annotate using the contents from the specified file. Can read from standard input with - . |
--date <format> | Specifies the format used to output dates. |
--[no-]progress | Enable or disable progress reporting. Cannot be used with --porcelain or --incremental . |
-M[<num>] | Detect moved or copied lines within a file. <num> specifies the minimum number of alphanumeric characters for detection (default: 20). |
-C[<num>] | Detect moved or copied lines between files. Can be specified multiple times. <num> specifies the minimum number of alphanumeric characters for detection (default: 40). |
--ignore-rev <rev> | Ignore changes made by the specified revision when assigning blame. Can be specified multiple times. |
--ignore-revs-file <file> | Ignore revisions listed in the specified file. Can be repeated. |
--color-lines | Color line annotations differently if they come from the same commit as the preceding line. |
--color-by-age | Color line annotations based on the age of the line. |
-h | Show help message. |
-c | Use the same output mode as git-annotate . |
--score-debug | Include debugging information related to line movement between or within files. |
-f , --show-name | Show the filename in the original commit. |
-n , --show-number | Show the line number in the original commit. |
-s | Suppress the author name and timestamp from the output. |
-e , --show-email | Show the author email instead of the author name. Controlled by blame.showEmail config option. |
-w | Ignore whitespace when comparing the parent’s version and the child’s. |
--abbrev=<n> | Use the specified number of hexadecimal digits for abbreviated object names. |
Examples
1. Blame a Specific File
To see who modified each line of a specific file, simply run:
$ git blame example.txt
This will display the author, date, and commit hash for each line in example.txt
.
2. Blame a Specific Line Range
If you only want to see who modified a specific range of lines in a file, you can use the -L
option:
$ git blame -L 10,20 example.txt
This will display the blame information for lines 10 to 20 in example.txt
.
3. Blame with Additional Options
You can also use the -c
, -w
, and -M
options to customize the output:
-c
: Displays the original commit for lines that have been moved or copied.-w
: Ignores changes in whitespace.-M
: Detects lines that have been moved within a file.
For example, to blame a file and ignore whitespace changes:
$ git blame -w example.txt
This will display the blame information for example.txt
, ignoring any changes in whitespace.