git blame
show what revision and author last modified each line of a file.
Syntax
Below is the syntax of this command.
git blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental] [-L <range>] [-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>] [--ignore-rev <rev>] [--ignore-revs-file <file>] [--color-lines] [--color-by-age] [--progress] [--abbrev=<n>] [<rev> | --contents <file> | --reverse <rev>..<rev>] [--] <file>
-L <start>,<end> annotate only the line range given by range <start>,<end>. -L <start> or -L <start>, spans from <start> to end of file. -L :<funcname> annotate the function name regex <funcname>.
Git Blame vs. Git Annotate
git annotate
is similar to git blame
. The only difference between this command and git-blame is that they use slightly different output formats.
Git Blame vs. Git Log
Git log you use it to learn about the history of a project. Below command will show all the commits that touched a given file.
git log -- <FILE-PATH>
git log gives you the overall history of the file through the entire timeline. Git blame, on the other hand, tells you who was the last one to touch each line of the code.
Example
To find the author and commit information of each line of the last modified version of the file, run git blame as follows:
git blame options.cpp
To specify the revision along with the line number, use below command
git blame -L 10,+1 fe25b6d -- options.cpp
Above command will show the revision for the file opions.cpp starting from line 10 till 11 (10 +1) for revision specified by commit fe25b6d. --
is used as a separator in command line arguments. In the case of Git, it’s usually used to separate things like commit hashes from a list of filenames.
Following example show blame commits that are more than 3 days old.
git blame --since=3.days -- options.cpp