PLAINFINCH learn by building

Looking back: log and diff

You will be able to read your project history and see exactly which lines changed before you commit.

How do I see the history of a project?

git log lists commits on your current branch, newest first. Each entry shows the full commit id, the branch labels pointing at it, the author and the message. On the bird club site you will see the start of the site, the kingfisher sighting and the June walk announcement.

That full form is tall. git log --oneline gives one line per commit: the short id and the summary line. Add -n 2 (or -2) to show only the two most recent. On a real machine a long log opens in a pager; press Space to scroll and q to quit.

The labels in brackets matter. (HEAD -> main) means you are on the branch main and it points at this commit. Later you will also see labels such as origin/main, the remote's copy of a branch.

git log
git log --oneline
git log --oneline -n 2

What do HEAD and HEAD~1 mean?

HEAD is Git's name for the commit you are on, normally the newest commit of your branch. Many commands accept it wherever they accept a commit id, so you rarely need to copy ids by hand.

HEAD~1 means one commit before HEAD, HEAD~2 two before, and so on. HEAD^ is another spelling of HEAD~1. They are relative, so after your next commit HEAD~1 points at a different commit than it does now.

You can also use the short ids that git log --oneline prints. Seven characters is normally enough. If you type an id that does not exist, Git says unknown revision and the simulator suggests checking the log.

git show HEAD
git show HEAD~1

How do I read a diff?

A diff is Git's description of how one version of a file became another. It starts with a header naming the file, then shows lines with a one-character prefix. A + line was added, a - line was removed, and lines with a space in front are unchanged context shown so you can see where the change sits.

A change to a line shows up as a removal followed by an addition. When the June walk moves from 8am to 9am, the diff shows - with the old sentence and + with the new one. Read both together.

Real Git also prints @@ markers with line numbers and colours additions green and removals red. The practice terminal keeps the + and - prefixes, which carry the meaning, and leaves out the line-number markers.

git diff

Which diff compares what?

Diff always compares two of Git's places. Which two depends on the form you use, and confusing them is the reason many people think their diff is empty.

  • git diff: working folder against the staging area. It shows edits you have not staged yet. Once you stage everything it shows nothing.
  • git diff --staged (also spelled --cached): staging area against the last commit. It shows exactly what git commit would record right now.
  • git diff HEAD: working folder against the last commit, staged or not.
  • git diff HEAD~2 HEAD: two commits against each other. Add a path at the end, such as index.html, to limit any diff to one file.
FigureBird club site: git diff compares working folder with staging, git diff --staged compares staging with the last commit, git diff HEAD compares folder with last commit. Below, the diff shows TEST TEST and Little egret added.Bird club site: git diff compares working folder with staging, git diff --staged compares staging with the last commit, git diff HEAD compares folder with last commit. Below, the diff shows TEST TEST and Little egret added.
Each form of git diff compares two places. Plain git diff shows unstaged edits as + and - lines, which is how the stray TEST TEST line shows up before it reaches a commit.

When should I check a diff?

Before every commit, run git diff --staged. It takes a few seconds and catches the things that otherwise end up in history: a debugging line, a TEST paragraph, a deleted section you meant to keep, a password pasted into a config file. The task on this page is that habit in miniature.

Also check after an AI tool edits your files. Assistants sometimes change lines you did not ask about, reformat a whole file, or remove a block they considered unused. git diff shows every one of those edits, however small, before you accept them into a commit.

And use it when something broke. Compare the last version that worked with the current one: git diff HEAD~1 index.html shows what changed in one step.

git add index.html
git diff
git diff --staged

How do I look inside one old commit?

git show <commit> prints the commit's details followed by the diff between it and its parent: exactly what that commit changed. git show HEAD~1 on the bird club site shows the line that announced the June walk.

With no argument, git show shows the latest commit. It is a quick way to confirm that your last commit contains what you think it does, right after making it.

To see an older version of a whole file rather than the change, real Git has git show HEAD~2:index.html. The practice terminal shows the change only; the restore lesson covers getting an old file back.

git show
git log --oneline
git diff HEAD~2 HEAD index.html

How do the Graph and Areas views relate to these commands?

The Graph view is git log drawn as pictures: each dot is a commit, newest at the end, with main and the ◆ marker on the commit you are on. When you commit, a new dot appears and the label moves.

The Areas view shows which files differ between the working folder, the staging area and the last commit, the same comparison the three forms of git diff print as text. A file marked changed in the working folder is one git diff will show; a staged file is one git diff --staged will show.

Use the views to build intuition, then rely on the text. On your own machine you will have the commands only, and VS Code's Source Control panel is another picture of the same information.

What should I check in history after an AI tool has been working?

Coding assistants change files fast. Git gives you a precise record of what they did, if you look.

  • Run git log --oneline -n 5 and make sure every commit is one you expected. Some agents commit on their own.
  • Run git show on each new commit and read the diff, especially deletions.
  • Look for whole-file changes. A diff that removes and re-adds every line usually means line endings or indentation changed, which hides the real edit.
  • Check for secrets in + lines: keys, tokens, passwords and private email addresses.

Field notes

  • git log lists the commits on your branch, newest first.
  • In a diff, lines starting with + were added and lines with - were removed.
  • git diff shows unstaged edits, and git diff --staged shows what will be committed.
  • Checking the staged diff before each commit keeps stray test lines out of history.

Easy to get wrong

Plain git diff only shows unstaged edits. Staged changes appear with --staged.

It is the last easy moment to catch test lines, deletions and secrets before they become part of history.

Relative names and short ids are quicker to type and harder to mistype.

Questions people ask

How do I leave the log screen on my own computer?

Press q. Real Git shows long output in a pager called less. Space scrolls down, b scrolls up, and /word searches.

Can I see who changed a particular line?

On real Git, git blame index.html shows the commit and author for each line. The practice terminal does not support it.

Why does a diff show a whole file as changed when I edited one line?

Usually line endings or indentation changed across the file, often by an editor setting or an auto-formatter. Check your editor's line ending setting.

Does git log show commits from other branches?

Only the current branch by default. Add --all to include every branch, which becomes useful in the branches lessons.

Keep going