git blame <path>

Show who last changed each line of a file and in which commit.

This command is useful for understanding the history of specific lines in a file, helping you track down when and why a change was made and by whom, by annotating each line with commit and author information.

This command runs git’s blame/annotate mechanism on the file specified by <path>, showing for every line the abbreviated commit hash, the author, the timestamp, and the line content; <path> is the file you want to inspect, such as app/models/user.rb or src/index.js. By default it uses the current HEAD to determine line history, walking backwards through commits to find the last change for each line and printing them in the file’s current order. You can add -L <start>,<end> to limit the output to a specific line range (e.g. -L 20,60 to only show lines 20–60), or -L <funcname> to blame just a function if Git can detect it. The flag -e tells it to show author emails instead of just names, while -n forces display of the line numbers in the original file; using both as git blame -e -n <path> combines these details. The flag --reverse <rev1>..<rev2> inverts the view to show when lines originated in a specific range of commits, and -C or -M can be added to have Git track code movement or copies across files or within the same file when blaming lines.

You can quickly refine this command by adding flags that narrow the history or change the format: git blame -L 50,80 <path> focuses only on lines 50–80, while git blame -e <path> helps when you care about the author’s email (for scripts or audit tooling). For cleaner, more script-friendly output you can use git blame --porcelain <path>, which prints a more detailed but machine-readable format. To see who changed lines between specific commits, use git blame <rev> -- <path> (for example git blame HEAD~3 -- <path>) so the file is annotated as it looked at that revision. If you need more context around the change for a specific line, combine this command with git show <commit> or git log -L <start>,<end>:<path> to inspect the full diff and commit message that introduced or last modified those lines.

Examples:

  • git blame app/models/user.rb
  • git blame -L 20,60 app/controllers/users_controller.rb
  • git blame -e -n src/index.js
Related commands

Welcome to GitExamples!

Sign in to gitexamples