git blame
is used to examine the history of a file, showing line-by-line revisions alongside the name of the last person who modified each line, the commit hash, and the timestamp of the change. This functionality is useful for developers seeking more context on why certain changes were made, and for identifying who made specific changes for further discussion.
How to Use git blame
Basic Usage
To use git blame
, execute the git blame
command followed by the filename of the file you wish to examine:
git blame README.md
This will display the README.md file's content in the terminal, with each line prefixed by the commit hash, author, date, and the line number.
For example you might see something like this:
a1b2c3d4 (Jane Doe 2022-01-15 12:34:56 +0000 1) # Project Titlee5f6g7h8 (Alex Smith 2022-02-20 14:30:22 +0000 2) A brief description of the project.a9b8c7d6 (Alex Smith 2022-03-10 16:45:33 +0000 3)d5e6f7g8 (Jane Doe 2022-04-05 18:50:44 +0000 4) ## Featuresg1h2i3j4 (Jane Doe 2022-05-25 20:55:55 +0000 5) - Feature 1k5l6m7n8 (Charlie Brown 2022-06-15 23:00:00 +0000 6) - Feature 2b3c4d5e6 (Jane Doe 2022-07-05 08:05:06 +0000 7)f7g8h9i0 (Alex Smith 2022-08-10 10:10:10 +0000 8) ## Installationj1k2l3m4 (Alex Smith 2022-09-20 12:15:20 +0000 9) Instructions on how to install the project.e5f6g7h8 (Jane Doe 2022-10-30 14:20:30 +0000 10)a1b2c3d4 (Charlie Brown 2022-11-25 16:25:40 +0000 11) ## Contributingd5e6f7g8 (Charlie Brown 2022-12-15 18:30:50 +0000 12) Guidelines for contributing to the project.
In this output:
- Each line of the
README.md
file is shown. - The commit hash (e.g.,
a1b2c3d4
) of the last commit that modified the line is displayed first. - This is followed by the name of the author who made the commit (e.g.,
Jane Doe
), and the date and time of the commit (e.g.,2022-01-15 12:34:56 +0000
). - The line number within the file is then shown in parentheses (e.g.,
1)
). - Finally, the content of the line itself is displayed (e.g.,
# Project Title
).
Advanced Options
Specific Line or Range: To focus on a specific line or a range of lines, use the
-L
option:Terminalgit blame -L 50,60 README.mdThis command will show details for lines 50 to 60 of the README.md file.
Show Commit Message: Adding the
-s
option shows the commit messages, helping to understand the context of changes:Terminalgit blame -s README.mdIgnoring Whitespace: Use
-w
to ignore whitespace changes, focusing on substantive changes to the file:Terminalgit blame -w README.md
Tips and Tricks
- GUI Tools: Many graphical Git tools, and IDEs, such as the JetBrains suite, VS Code, or the Graphite web app, offer a
blame
feature that visualizesgit blame
information in a more user-friendly manner. - Investigating Changes: Use
git blame
in conjunction withgit log
andgit diff
for a comprehensive view of a file's history and changes over time. - Dealing with Renames: If a file was renamed, use the
--follow
option to track its history across the rename.
For more information see the official documentation on Git blame.