Building it · lesson 3 of 10
Ideas in this lesson
Agit status / git diffwhat’s changed, and how
Bgit addchoose what goes in the next commit
Cgit commit -msave a snapshot, with a message
Dgit logread the history

Git: commits

Last updated

A commit is Git's saved version: a snapshot of the project, with who made it, when and why. In this lesson you'll use Git for real on Pila's repository: see what's changed, choose what goes into a commit, write a message a teammate can use, and read the history.

By the end of this lesson you can

  • Name Git's three places: the working folder, the staging area and the repository
  • Use git status, add, commit, log and diff
  • Make small commits, each with a message that says what changed and why

Three places

Git keeps track of three places:

  • The working folder: the files as you see and edit them.
  • The staging area: what will go into the next commit. You choose what's in it.
  • The repository: the commits, kept in a hidden .git folder.

Five commands move work between them and show you where it is:

Five everyday Git commands
CommandWhat it does
git statusLists what’s changed in the folder, and what’s staged
git diffShows changed lines not yet staged (add --staged for staged ones)
git add fileCopies the file as it is now into the staging area
git commit -m "message"Saves everything staged as a new commit, with the message
git logLists the commits, newest first (add --oneline for one line each)

The staging area is what lets one commit hold one change, even when you've changed several things.

Commit two changes, separately

You've joined the Pila team. Marco and Bea have made two commits; you've written sms.py and, separately, improved the README. They're two unrelated changes, so they belong in two commits. The sandbox draws the three places and the history as you go.

Git sandbox
  1. See what’s waiting with git status
  2. Look at the README change with git diff
  3. Stage and commit only sms.py, with a message that says what it adds
  4. Then stage and commit the README change in its own commit
  5. Read the history with git log --oneline

Working folder

Staging area

nothing staged

Repository

2 commits on main

You are in ~/pila, Pila’s repository, with two commits from Marco and Bea. You have a new file, sms.py, and a change to README.md, neither committed yet.

Commit graph

  1. 6c3d6f9 HEAD →main Give out numbers, one per phone, up to 60 a day
  2. 389ecdd Start Pila with a README

The last task also checks your two messages: each should start with a capital letter and a command like "Add" or "Explain", say what changed, and stay under 50 characters. If you committed both files together, press the sandbox's reset and try again: that's this lesson's trap.

A commit is a snapshot with a parent

Each commit records the whole project as it was, a snapshot, not just the lines that changed. Git stores unchanged files once, so this costs little. Each commit also points to the commit before it, its parent, which is how the history forms a chain. Every commit has an id, a hash like a3f9c1…, made from its contents, so a commit can't be changed without its id changing.

That chain is what lets git log show the history, git diff compare any two commits, and Git undo a change by going back to an earlier snapshot. Simple Git goes further: Your first repository, The staging area, Ignoring files, Reading history, Comparing versions and Undoing commits.

Messages a teammate can use

Six months from now someone will read your message to find out why a line changed. Widely used conventions:

  • A subject line under about 50 characters, in the imperative, as if completing "This commit will…": "Add the reply text for a new number".
  • If needed, a blank line, then a body saying why: the requirement, the bug, the decision.
  • One change per commit. If the message needs "and", it's probably two commits.

Simple Git's Good commits and messages and Commit conventions say more.

Close the queue at 17:00, not 16:30

Ate Cora's rule from the requirements (3.P): the centre
serves patients until 5 p.m. Numbers taken after 17:00
are refused with the "closed" reply.

Spot the trap: “stuff”

Spot the trapMarco

At the end of a long day, Marco has fourteen changed files: the opening hours, a typo in the README, a new reply text, a half-finished cancel feature and some debugging prints. He types git commit -am "stuff".

What happens if…

-a stages every changed tracked file at once, skipping the choice the staging area exists for. It's handy for a single small change, and risky at the end of a day.

Check what you learned

You changed two files for two different reasons. What do you do?
What is in the staging area?
Which is the best subject line?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development