annotate Git Command Guide
The git annotate command (an alias for git blame) annotates each line in a file with the commit information that most recently modified it. This is useful for determining when and by whom changes were made, aiding in code reviews and debugging. It shows commit IDs, authors, timestamps, and the actual lines.
git annotate Syntax:
Section titled “git annotate Syntax:”git annotate [options] [rev-opts] [rev] [--] <file>Options:
Section titled “Options:”| Option | Description |
|---|---|
| -b | Do not show object names for boundary commits |
| -l | Annotate only the first line of each commit |
| -t | Show raw timestamp instead of relative time |
| -S | Use revs from the specified file for blame |
| —show-stats | Include additional statistics |
| -p | Show additional info for each line |
| -e | Show email instead of author name |
| —since= | Show changes more recent than date |
| -L <start,end> | Annotate only lines from start to end |
| -C | Detect moved or copied lines within the same file |
| -M | Detect moved or copied lines across files |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| rev | Commit to examine |
| file | File to annotate |
git annotate Command Samples:
Section titled “git annotate Command Samples:”Annotate a file at its current state
Section titled “Annotate a file at its current state”git annotate file.txtShows the last commit that modified each line in file.txt.
Annotate with author email
Section titled “Annotate with author email”git annotate --email file.txtDisplays author emails instead of names.
Annotate specific lines
Section titled “Annotate specific lines”git annotate -L 10,20 file.txtAnnotates only lines 10 through 20.
Show raw timestamps
Section titled “Show raw timestamps”git annotate -t file.txtDisplays absolute timestamps instead of relative ones.
Detect moved lines across files
Section titled “Detect moved lines across files”git annotate -M file.txtAttempts to find lines that were moved or copied from other files.
How do I use git annotate to find who changed a specific line?
Section titled “How do I use git annotate to find who changed a specific line?”To find the author and commit for a specific line, use:
git annotate -L <line-number>,<line-number> <file>How can I see the email addresses instead of names in git annotate?
Section titled “How can I see the email addresses instead of names in git annotate?”To display email addresses for authors in git annotate, execute:
git annotate -e <file>How do I limit git annotate to a specific range of lines?
Section titled “How do I limit git annotate to a specific range of lines?”To limit the annotation to a specific range of lines, use the following syntax:
git annotate -L <start-line>,<end-line> <file>How can I get raw timestamps with git annotate?
Section titled “How can I get raw timestamps with git annotate?”To get raw timestamps instead of relative times, use:
git annotate -t <file>How do I detect copied or moved lines using git annotate?
Section titled “How do I detect copied or moved lines using git annotate?”To detect copied or moved lines within the same file, use:
git annotate -C <file>How can I show additional statistics with git annotate?
Section titled “How can I show additional statistics with git annotate?”To include statistics about the annotated file, use:
git annotate --show-stats <file>Applications of the git annotate command
Section titled “Applications of the git annotate command”- Viewing authorship information per line in a file
- Finding changes made within a specific time frame
- Displaying author email addresses instead of names
- Annotating only a specific range of lines in a file
- Detecting moved or copied lines within the same file
- Detecting moved or copied lines across different files
- Showing raw timestamps for each line change