Bird
Raised Fist0
Gitdevops~5 mins

git blame for line-by-line history - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Sometimes you want to know who last changed each line in a file and when. Git blame helps you find that out by showing the author and commit for every line in a file.
When you want to find out who wrote or last changed a specific line in a file.
When you need to understand why a particular line of code was added or modified.
When you are reviewing code and want to ask the right person about a change.
When debugging and you want to trace the history of a line that causes a problem.
When preparing documentation or release notes and want to credit contributors.
Commands
This command shows who last changed each line in the file example.txt, along with the commit hash and timestamp.
Terminal
git blame example.txt
Expected OutputExpected
a1b2c3d4 (Alice 2024-06-01 10:15:30 +0000 1) First line of the file b5e6f7g8 (Bob 2024-06-02 14:20:10 +0000 2) Second line of the file c9d0e1f2 (Alice 2024-06-03 09:05:00 +0000 3) Third line of the file
This command limits the blame output to lines 2 through 3 in example.txt, so you only see the history for those lines.
Terminal
git blame -L 2,3 example.txt
Expected OutputExpected
b5e6f7g8 (Bob 2024-06-02 14:20:10 +0000 2) Second line of the file c9d0e1f2 (Alice 2024-06-03 09:05:00 +0000 3) Third line of the file
-L 2,3 - Limits output to lines 2 through 3
This command shows the blame output in a detailed, machine-readable format with extra metadata for each line.
Terminal
git blame -p example.txt
Expected OutputExpected
a1b2c3d4 1 1 1 author Alice author-mail <alice@example.com> author-time 1685600130 author-tz +0000 committer Alice committer-mail <alice@example.com> committer-time 1685600130 committer-tz +0000 summary Initial commit filename example.txt b5e6f7g8 2 2 1 author Bob author-mail <bob@example.com> author-time 1685690410 author-tz +0000 committer Bob committer-mail <bob@example.com> committer-time 1685690410 committer-tz +0000 summary Added second line filename example.txt c9d0e1f2 3 3 1 author Alice author-mail <alice@example.com> author-time 1685778300 author-tz +0000 committer Alice committer-mail <alice@example.com> committer-time 1685778300 committer-tz +0000 summary Added third line filename example.txt
-p - Shows detailed, porcelain format output
Key Concept

If you remember nothing else from git blame, remember: it shows who last changed each line in a file and when.

Common Mistakes
Running git blame on a file that does not exist or is not tracked by git.
Git blame needs a tracked file to show history; otherwise, it will error out.
Make sure the file exists and is committed in the git repository before running git blame.
Not using the -L flag when wanting to see blame for only specific lines.
Without -L, git blame shows the entire file, which can be overwhelming for large files.
Use git blame -L start,end filename to focus on the lines you care about.
Confusing git blame output with current file content only.
Git blame shows the last commit that changed each line, not just the current content, so lines may come from different commits.
Read the commit hash and author info to understand the history behind each line.
Summary
Use git blame filename to see who last changed each line in a file.
Use git blame -L start,end filename to limit output to specific lines.
Use git blame -p filename for detailed metadata about each line's history.

Practice

(1/5)
1. What does the git blame command primarily show?
easy
A. Who last changed each line in a file
B. The current status of the repository
C. The list of branches in the repository
D. The commit history summary

Solution

  1. Step 1: Understand the purpose of git blame

    git blame is used to show the author information for each line in a file, indicating who last modified that line.
  2. Step 2: Compare with other git commands

    Other commands like git status, git branch, and git log serve different purposes, not line-by-line author tracking.
  3. Final Answer:

    Who last changed each line in a file -> Option A
  4. Quick Check:

    git blame = line author info [OK]
Hint: Remember: blame = who changed each line last [OK]
Common Mistakes:
  • Confusing git blame with git log
  • Thinking git blame shows branch info
  • Assuming git blame shows file status
2. Which of the following is the correct syntax to show blame for a file named app.js?
easy
A. git blame --file app.js
B. git blame app.js
C. git blame --show app.js
D. git blame -f app.js

Solution

  1. Step 1: Recall basic git blame syntax

    The basic syntax to run blame on a file is simply git blame <filename>. No extra flags are needed for a simple blame.
  2. Step 2: Check the options given

    Options like -f, --show, or --file are not valid or required for basic blame usage.
  3. Final Answer:

    git blame app.js -> Option B
  4. Quick Check:

    Basic blame = git blame filename [OK]
Hint: Use just 'git blame filename' for simple blame [OK]
Common Mistakes:
  • Adding unnecessary flags
  • Using incorrect or unsupported options
  • Confusing blame syntax with other git commands
3. Given the command git blame -L 10,15 README.md, what will it show?
medium
A. Blame info for lines 1 to 15 of README.md
B. Blame info for the whole README.md file
C. Blame info for lines 10 to 15 of README.md
D. An error because -L requires a commit hash

Solution

  1. Step 1: Understand the -L option in git blame

    The -L option limits the blame output to a specific line range. Here, -L 10,15 means lines 10 through 15.
  2. Step 2: Apply to the file README.md

    The command will show blame info only for lines 10 to 15 of the file README.md, not the entire file or any other range.
  3. Final Answer:

    Blame info for lines 10 to 15 of README.md -> Option C
  4. Quick Check:

    -L limits lines = lines 10-15 [OK]
Hint: -L start,end shows blame only for those lines [OK]
Common Mistakes:
  • Assuming -L shows whole file blame
  • Thinking -L needs a commit hash
  • Confusing line numbers with byte offsets
4. You run git blame --since=2.weeks README.md but get an error. What is the likely cause?
medium
A. You need to specify a commit hash with --since
B. README.md file does not exist
C. Incorrect date format for --since option
D. git blame does not support --since option

Solution

  1. Step 1: Check if git blame supports --since

    The git blame command does not have a --since option. This option is valid for git log but not for blame.
  2. Step 2: Understand the error cause

    Using an unsupported option causes git blame to error out. The file existence or commit hash is unrelated here.
  3. Final Answer:

    git blame does not support --since option -> Option D
  4. Quick Check:

    git blame lacks --since option [OK]
Hint: git blame has no --since; use git log for date filters [OK]
Common Mistakes:
  • Assuming git blame supports --since
  • Blaming file existence without checking
  • Thinking commit hash is mandatory with --since
5. You want to find who last changed line 42 in server.py but only for commits on the feature branch. Which command correctly achieves this?
hard
A. git blame feature -- server.py -L 42,42
B. git blame -L 42,42 server.py feature
C. git blame -L 42,42 -- server.py feature
D. git blame -L 42,42 --first-parent feature -- server.py

Solution

  1. Step 1: Understand branch limitation in git blame

    To blame a file as it appears on a specific branch, specify the branch name before the -- separator, then the file name.
  2. Step 2: Apply line range and branch correctly

    The correct syntax is git blame <branch> -- <file> -L <start,end>. So git blame feature -- server.py -L 42,42 limits blame to line 42 on the feature branch.
  3. Final Answer:

    git blame feature -- server.py -L 42,42 -> Option A
  4. Quick Check:

    Branch before --, file after, -L for lines [OK]
Hint: Put branch before --, file after, use -L for lines [OK]
Common Mistakes:
  • Placing branch after file name
  • Misordering -L option
  • Using unsupported options like --first-parent with blame