Lesson 4 of 7 in Basics

Reading history

Story: Pim can tell you what changed in the archive on any day in its history. Now you can too.

Git keeps every commit you make, so a repository holds its whole history. This Lesson's commands read that history. They only look: they never change your files or your commits, so they're always free and never count toward Par.

Listing commits with git log

git log lists the commits of the branch you're on, newest first. In a project with three commits, it starts like this:

commit a4b8d971a4104bc200dc7415344326f408c54d03 (HEAD -> main)
Author: You <you@example.com>
Date:   Mon Jan 1 09:10:00 2024 +0000

    Style the heading

commit ab89288b359896d924d7c8207edaac72b537b3cd
Author: You <you@example.com>
Date:   Mon Jan 1 09:07:00 2024 +0000

    Add the home page

Each commit shows:

  • its id, the long line of letters and digits. Git works it out from the commit's content, and no two commits have the same one;
  • its labels, such as (HEAD -> main): you're on main, and this is its newest commit;
  • who made it, and when;
  • its message.

One line per commit: --oneline

git log --oneline shows each commit on one line: the first 7 characters of its id, its labels and its message.

a4b8d97 (HEAD -> main) Style the heading
ab89288 Add the home page
37d7cf7 Add a README

The short id is enough to name a commit. Git only needs the start of an id, as long as no other commit's id starts the same way.

What a commit changed: git show

git show <commit> shows one commit: the same details as git log, then the lines the commit changed. This is called a diff:

diff --git a/index.html b/index.html
index 284fccb..84c05d5 100644
--- a/index.html
+++ b/index.html
@@ -2,3 +2,4 @@
 <title>Home</title>
 <h1>Hello</h1>
 <p>Welcome to my website.</p>
+<p>Open on Mondays.</p>
  • A line starting with + was added.
  • A line starting with - was removed. A changed line shows up as its old version removed and its new version added.
  • Lines with neither didn't change. They're there to show where the change is.
  • The lines above them say which file it is, and where in the file the change starts.

On its own, git show shows the newest commit on the branch you're on: the one HEAD points at. To see another commit, give its id: git show ab89288.

Every branch: --all and --graph

A repository can have more than one branch: another line of commits, with its own label. You'll make branches yourself in Branching & Merging. The Live Graph draws every branch, but git log lists only the branch you're on.

  • git log --all lists the commits of every branch.
  • git log --graph draws lines on the left that show how the commits connect, and where a branch splits off.

The options combine. git log --oneline --graph --all draws a small map of the whole repository, much like the Live Graph:

* a6b0f1b (HEAD -> main) Make the heading purple
| * a1a97c1 (hours) Add opening hours
|/
* a4b8d97 Style the heading
* ab89288 Add the home page
* 37d7cf7 Add a README

Here the branch hours splits off after Style the heading, and its commit Add opening hours isn't on main. A branch's name works wherever an id does: git show hours shows the newest commit on hours.

On your own computer

When the output is longer than your screen, git shows it one page at a time: press Space for the next page, and q to stop reading. The Terminal here shows everything at once.

Command What it does
git log Lists the commits of the branch you're on, newest first: id, author, date and message.
git log --oneline The same, one line per commit.
git log --all Lists the commits of every branch.
git log --graph Draws how the commits connect.
git show Shows the newest commit and the lines it changed.
git show <commit> Shows that commit: name it by its id, or by a branch for that branch's newest commit.

New commands

  • git log
  • git log --oneline
  • git log --graph
  • git log --all
  • git show

Explainer: Reading the history

project: your repo, you are here37d7cf7 Add a README37d7cf7ab89288 Add the home pageab89288a4b8d97 Style the headinga4b8d97a1a97c1 Add opening hoursa1a97c1a6b0f1b Make the heading purplea6b0f1bHEAD → mainhours
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → main at a6b0f1b "Make the heading purple"

Branches and tags

  • hours at a1a97c1 "Add opening hours"
  • main at a6b0f1b "Make the heading purple"

Commits, newest first

  1. a6b0f1b "Make the heading purple"; parent a4b8d97; refs: HEAD → main
  2. a1a97c1 "Add opening hours"; parent a4b8d97; refs: hours
  3. a4b8d97 "Style the heading"; parent ab89288
  4. ab89288 "Add the home page"; parent 37d7cf7
  5. 37d7cf7 "Add a README"; no parent

Challenge 1 of 3

Story: Someone trimmed a line from our records, Agent. The history remembers what it said.

The last commit removed a line from index.html by mistake. Put that line back, exactly as it was, and commit the fix.

Commands 0 of Par 3

Goal

  • Met: HEAD is on branch main
  • Not met yet: Branch main is at the goal's commit
  • Not met yet: The history of main matches the goal
  • Not met yet: main has one new commit
  • Not met yet: The new commit has the welcome line back under the heading in index.html

Hints

Live Graph

project: your repo, you are here37d7cf7 Add a README37d7cf7ab89288 Add the home pageab89288a4b8d97 Style the headinga4b8d97f1d3ce4 Tidy the home pagef1d3ce4HEAD → main

Terminal

~/project (main) $