Git Blame
The git blame
command is a flexible tool for troubleshooting that has several usage options.
The git blame command's main function is to display the last author metadata, and the last commit attached to each line in a file.
The git blame command will be useful when you want to see which commit is responsible for the modifications and which author committed those modifications.
The git blame command is used to answer questions about what, how, and why the modifications were introduced to the git repository.
How to use git blame
The git blame
command is simple to use. It works on individual files.
To inspect a file, we pass the name of the file to the git blame command using the following syntax :
$ git blame <file_path>
The example below shows the output generated by the git blame command on a file.
$ git blame file1.txt
86b5548f (john williams 2019-12-11 16:36:36 +0100 1) hello world
4b3251c5 (emma miller 2019-12-14 23:06:14 +0100 2) Git is awesome
c3bf48e6 (william anderson 2019-12-17 22:54:46 +0100 3) More changes here for test
Listing a specific range of lines
The -L
will limit the output to the only specific ranges of lines of a file.
The syntax for displaying a specific range using the -L
option will be as follows:
$ git blame -L <start_line_number>,<end_line_number> <file_path>
For example, to show lines 5-15 from the README.md file, type the following command:
$ git blame -L 5, 15 README.md
To show a certain number of lines after a specific line, follow the syntax below:
$ git blame -L <starting_line_number>,+<how_many_lines_to_show> <file_path>
For example, to display 3 lines starting from line 20 of the README.md file, run the following command:
$ git blame -L 20,+3 README.md
Displaying the author email
The -e
option outputs the author's email address instead of the default username.
To see the author email, type the git blame with -e
option as follows:
$ git blame -e README.md
Ignoring the whitespace
The -w
option ignores whitespace modifications.
Unfortunately, when the modification is just the spacing of a file, the git blame command shows those changes. This is where -w
comes in handy to ignore whitespace.
To ignore whitespace changes, run the following command:
$ git blame -w README.md
Displaying date & time as a timestamp
The -t
option shows date & time as timestamp values instead of the default formatted date & time.
To use the -t
option, run the following command:
$ git blame -t README.md
Displaying long commit hash
The -l
option shows the full length commit hashes instead of the default short commit hashes.
To use the -l
option of git blame, run the following command:
$ git blame -l REAMDE.md
Display the original author when lines are moved on the same file
The -M
option recognizes copied or moved lines in the same file. So it will indicate the original author of the
lines instead of the author that copied or moved the lines.
To use the -M
option of git blame, run the following command:
$ git blame -M README.md
Display the original author when lines are moved on other files
The -C
option recognizes copied or moved lines from other files. So It will indicate the original author
of the lines instead of the author that copied or moved the lines.
To use the -C
option of git blame, run the following command:
$ git blame -C README.md
Git blame VS git log
The git blame is an entry-level history search. It displays the last author who changed a line, but in some situations, you may need to know when a line was initially added and by who. It's difficult to do with git blame; instead, you can use the git log command.
The -S
option of the git log command lets you find all the commits that include a given string. It runs as follows :
$ git log -S "String that you want to search"