git-annotate - Linux


Overview

git-annotate is a powerful Git command used to annotate lines of a file with information about the commit, author, date, and commit message associated with each line change. It provides a detailed historical record of a file’s evolution.

Syntax

git annotate [options] <file>

Options/Flags

  • -a, –all: Show annotations for all lines.
  • -f, –follow: Annotate the code in a specified revision and follow it across renames.
  • -L : Limit annotations to the specified line range.
  • -n, –numstat: Print line numbers and optionally the count of insertions and deletions.
  • -S : Only annotate lines matched by the regex.
  • –first-parent: Only show the first parent commit for each line.
  • –max-count : Limit the number of commits shown for each line.
  • –since, –until: Filter annotations by commit date.

Examples

Basic usage:

git annotate main.py

Annotations for a specific revision:

git annotate -f <commit-id> main.py

Annotations within a line range:

git annotate -L 10-20 main.py

CSV output with line additions:

git annotate -n main.py > output.csv

Common Issues

  • Wrong commit history: Ensure the git history is accurate and complete.
  • Large files: Annotating large files can be slow; consider using the -a option with caution.
  • Missing dates: If commit dates are not shown, check if the --first-parent option is enabled.

Integration

  • git blame: Blame lines to a specific committer and date.
  • git difftool: View annotation differences between revisions.
  • git grep: Search for a pattern across annotations.

Related Commands