Seeing What Changed and Who Changed It
Two questions come up constantly once a project has any history at all. What did this particular commit do? And who wrote this line, and what were they thinking?
Git answers both in about ten seconds, and the second answer is the one that makes every argument in Chapter 2 about commit messages suddenly concrete.
One commit in full
You met this command in the last topic. git show prints a commit's details and its complete diff:
git show c8d1e07
This is how you inspect a change somebody describes to you in a sentence — or one you made yourself and cannot remember. The message says what it was for; the diff underneath says exactly what it touched.
Comparing two points
Sometimes the question is bigger than one commit: what has changed over the last week, or between two versions of the project?
git diff a3f9c21 c8d1e07
Git shows the combined difference between those two states, however many commits sit between them. Notice this is the same git diff from Chapter 2 — the command compares two things, and until now those two things were "the last commit" and "your folder".
Who wrote this line
You are reading index.html and one line makes no sense. Perhaps it is a strange piece of formatting, or a fact you cannot verify.
git blame index.html
Every line of the file gets a short hash, a name and a date in front of it. The line you are staring at names the commit that last changed it.
The command has an unfortunate name. In practice nobody uses it to assign fault, and on your own project every line is yours anyway. It is a lookup tool: line to commit.
From line to reason
The lookup on its own is only half of it. Take the hash blame gave you and run show on it:
git blame index.html git show 5b2a9f3
The first command finds the change. The second prints its message and diff, which is where the reason lives. On Sandpiper: blame attributes the meeting-time line to this morning's commit, and show explains that the club hall moved to Thursdays.
This is also the moment the whole "write real commit messages" argument pays off in a way you can feel. If that commit had said update, the two-step would end in a shrug.
What this replaces
Look back at the first page of this book. Four questions that a folder of copies could not answer: what changed, when, why, who.
git show answers what. The log and blame answer when. Both print the author, which answers who. And the message answers why, provided somebody bothered to write one.
That is the whole promise of Chapter 1, delivered by three commands you now know. Everything after this chapter is about doing more work more safely, not about answering those questions better.
- "
blameis for finding who to blame." It is for finding the change. On a solo project every line is yours; on a team, treating it as an accusation is a cultural mistake worth avoiding from day one. - "
git diffbetween two commits shows each commit's message." It shows the combined difference and nothing else. The steps in between are whatgit logprints. - "
blameshows who originally wrote the line." It shows the commit that last changed it. A reformatting commit that touched every line will happily claim all of them. - "I need the full forty-character hash for
show." Seven characters are enough, and that is what--onelineandblameboth print.
- The blame-then-show loop is the most useful investigative skill in this book, and it is what you will use to understand a codebase you have just joined.
- Comparing two commits is how you check what a week of work actually did — which is code review in miniature, and Chapter 8's version of it.
- This page is where Chapter 2's commit-message discipline stops being advice and becomes something you can feel the absence of.
Knowledge Check
Which command answers "what did this specific commit change"?
git showwith the commit's hash, which prints its message and diffgit status, which reports the state of every commit in the projectgit log, which prints the changed lines underneath each commit entrygit blame, which prints the full contents of the commit you name
What does git blame actually tell you about a line?
- Which commit changed that particular line most recently, and who made it
- Who originally introduced the line when the file was first created
- Whether the line contains a mistake that caused a problem later
- How many times the line has been edited since the project began
The blame-then-show loop ends with a commit message that says only "update". What has gone wrong?
- Nothing technical, since the reason was never recorded and cannot be recovered
- The commit is damaged, and the rest of the message needs to be recovered from a backup
- The wrong hash was used, so the loop found a different commit than intended
- Git truncated the message because it exceeded the fifty-character guideline
Which of the four questions from Chapter 1 does the commit message answer?
- Why the change was made, in the words of whoever actually made it
- What changed, since the message summarizes the modified lines
- When the change happened, since the message includes the date
- Who made the change, since the message is signed by its author
You got correct