Lesson 2 of 3 in Investigating
Who changed this line
Story: Every line in the archive carries a name and a date, Agent. Learn to read them.
You're looking at a line in a file and wondering where it came from. Who wrote it? When? Was it on purpose? git log answers questions about commits. git blame answers them about lines.
Like git log, it only reads. It never changes your files or your commits, so it's always free and never counts toward Par.
Reading git blame
git blame <file> prints the file, one line at a time, and puts the commit that last changed each line in front of it. Here is a café menu that three people have worked on:
$ git blame menu.txt
6bcfc7d1 (Maya Chen 2024-02-07 11:30:00 +0000 1) Soup 5
^b65f139 (You 2024-02-05 09:00:00 +0000 2) Bread 2
63f2191c (Leo Novak 2024-02-06 10:05:00 +0000 3) Pie 6
Each line starts with:
- the start of the commit's id. It's 8 characters long here, and you can use it wherever git wants a commit, just like the 7 characters
git log --onelineshows; - in brackets: who made that commit, when, and the line's number in the file;
- then the line itself.
A ^ before an id marks the very first commit in the history. Bread 2 hasn't changed since the menu was first written.
Lines you've changed but not committed yet show 00000000 and Not Committed Yet instead of a commit.
From a line to its commit
git blame tells you which commit to look at. git show then tells you what that commit did:
$ git show 6bcfc7d
commit 6bcfc7d1b3202db4d3b3bef2669a98fe978b8ef0 (HEAD -> main)
Author: Maya Chen <maya@example.com>
Date: Wed Feb 7 11:30:00 2024 +0000
Raise the soup price
diff --git a/menu.txt b/menu.txt
index 4d72bda..d9bfb56 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1,3 +1,3 @@
-Soup 4
+Soup 5
Bread 2
Pie 6
Now you know what the line said before (Soup 4, on the line starting with -), what else changed in the same commit, and the commit message, which is often the "why". That's usually what you were after: not someone to blame, but the reason behind a line, and the right person to ask about it.
When blame points at the wrong commit
git blame only shows the last change to each line. Often that's the change you care about. But sometimes the last change was small: someone fixed a typo, renamed something, or re-indented the whole file. Then blame names them for every line they touched, even though they didn't write any of it.
git show on that commit gives it away. After a re-indent, each - line and its + line say the same thing, only spaced differently. To find where the text really came from, search further back:
git log -S "<text>"lists the commits that added or removed the text. Re-indenting a line doesn't change how many times its text appears, so-Sskips commits like that and finds the one that wrote the line.git log -p -- <file>shows every change to the file. Read from the bottom up to find where the line first appears.
On your own computer
git blame has options this Terminal doesn't: -L 10,20 blames only lines 10 to 20, and -w ignores changes that only add or remove spaces. Many editors also show blame next to each line.
| Command | What it does |
|---|---|
git blame <file> |
Shows each line of the file with the commit that last changed it: its id, author and date. |
git show <commit> |
Shows what that commit changed: - lines as they were before, + lines as they are after. |
git log -S <text> |
Lists the commits that added or removed the text, skipping ones that only re-indented it or moved it within its file. |
New commands
git blame
Explainer: Who changed this line
Graph as text
project: your repo, you are here
- HEAD → main at 6bcfc7d "Raise the soup price"
Branches and tags
- main at 6bcfc7d "Raise the soup price"
Commits, newest first
- 6bcfc7d "Raise the soup price"; parent 63f2191; refs: HEAD → main
- 63f2191 "Add a pie"; parent b65f139
- b65f139 "Add the menu"; no parent
Challenge 1 of 3
Story: Someone set this price, Agent. Put a marker on the moment they did.
shipping.txt says express delivery costs 9.50. Create a branch express-price at the commit that set that price, so the team can look at it. Stay on main.
Unlocks after Branching & Merging.
Terminal
Unlocks after Branching & Merging.
This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.