Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Git commits: add, commit and log

After this lesson: I can record my changes as commits and read the history.

55 min lesson12 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

What Git does for a project

Git is a tool that records every version of the files in a project. It shows what changed, when it changed and who changed it. It matters because you can go back to any earlier version, so a mistake never costs you your work.

Think of a report you edit for weeks, with files called final, final2 and really-final. Git replaces that mess with one folder and a clear history.

By the end of this lesson

Note

I can record my changes as commits and read the history.

Key words

WordWhat it means
Repositorya project folder whose history Git keeps
Commita saved snapshot of the project, with a message
Staging areathe waiting place for changes before a commit
Logthe list of commits, newest first
Hashthe long code that names one commit

The cells students fill in are left blank in this preview.

A commit saves a snapshot of your project

A repository is a project folder whose history Git keeps. Each time you save a step of your work, Git stores a commit. A commit is a snapshot of the project, with a message that says why you made it.

Each commit points back to the one before it. The commits form a chain, and that chain is the history.

Git writes your name on every commit

You tell Git who you are once. It keeps the answer for the whole lab.

First make a repository called menu. The -b main part names its first branch main. Then set your name and address.

Make a repository called menu

Command
git init -b main menu

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Tell Git your name

Command
git config --global user.name "Student"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Tell Git your address

Command
git config --global user.email "[email protected]"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Add chooses what goes into the next commit

Git does not save every change on its own. You choose. The command git add copies a file's current state into the staging area. The command git commit then saves everything waiting there.

The command git status --short shows where each file stands. It puts two letters before each file name.

Worked example: what the two letters say

LettersWhat they meanWhen you see them
`??`Git has never seen this fileAfter you make a new file
`A ` (A, then a space)A new file is stagedAfter git add on a new file
` M` (a space, then M)A known file changed, and it is not stagedAfter you edit a file
`M ` (M, then a space)A known file changed, and it is stagedAfter git add on an edited file

The cells students fill in are left blank in this preview.

Make a file with two prices

Command
printf 'Latte 3.20\nTea 2.10\n' > menu/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask where the file stands

Command
git -C menu status --short

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage the file

Command
git -C menu add prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask again

Command
git -C menu status --short

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Save the first commit

Command
git -C menu commit -m "Add the first prices"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think git add saves their work into the history. It does not. It only stages the change. Nothing is in the history until you run git commit, so a staged file that is never committed can still be lost.

The log lists the commits, newest first

Edit the file, look at what changed, and commit again. The option -a on git commit stages every file Git already knows, so you skip git add. It does not stage new files.

Then read the history with git log. The option --format chooses what each line shows: %an is the author and %s is the message.

Add a third price

Command
printf 'Cake 3.50\n' >> menu/prices.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

See how much changed

Command
git -C menu --no-pager diff --stat

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit every known file

Command
git -C menu commit -am "Add cake"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read the log, one line each

Command
git -C menu --no-pager log --format="%an|%s"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

An example of the full log

Code: bash
commit 451fb8e70cc390c845b7fb933284a7b883acf945
Author: Student <[email protected]>
Date:   Sun Oct 25 23:53:39 2026 +0000

    Add cake

Four facts sit in every log entry

The plain git log shows each commit in four parts. The first line holds the hash, a 40-character code that names the commit for ever. The second line names the author. The third gives the date.

The message comes last, indented by four spaces. The example above is only an example: your hash and date differ. When Git prints to a terminal, the first line can also end with names such as (HEAD -> main).

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.