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

Free lesson

Saving your work with Git

After this lesson: I can save my work as commits and see its history.

45 min lessonNo commands

Who changed the price list?

On Monday the shop's website shows tea at 2.50. On Tuesday it shows 2.80, and nobody remembers changing it.

Somebody changed a file. Which change was it, who made it, and what did the file say before?

Almost every technical team answers those questions with a tool called Git.

By the end of this lesson

Note

I can save my work as commits and see its history.

Key words

WordWhat it meansExample
Repositorya folder whose history Git keeps
Commita saved snapshot of your changes, with a message
Staging areathe list of changes for the next commit
Logthe list of past commitsgit log --oneline
Diffthe lines that changed between two versions
Brancha separate line of commits

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

A commit saves a snapshot

Saving a file only keeps its newest version. A commit keeps every version you choose to save.

It takes two steps. git add prices.csv puts the change in the staging area. git commit -m "message" saves everything staged as one commit.

Each commit has an id, an author, a date and your message.

What Git prints as you save

Code: bash
$ git status
On branch main
Changes not staged for commit:
        modified:   prices.csv

$ git add prices.csv
$ git status
On branch main
Changes to be committed:
        modified:   prices.csv

$ git commit -m "Raise tea price to 2.80"
[main 4f2a9c1] Raise tea price to 2.80
 1 file changed, 1 insertion(+), 1 deletion(-)

A common mistake

Warning

Many people think git add saves their work in the history. It does not: it only stages the change. Nothing is in the history until you run git commit. Run git status to see which step you are on.

log and diff read the history

git log --oneline lists the commits, newest first, one a line.

git diff shows what changed. A line starting with - was taken out. A line starting with + was put in.

A good message such as "Raise tea price to 2.80" tells the next person why.

Reading the price list's history

Code: bash
$ git log --oneline
4f2a9c1 Raise tea price to 2.80
b81d307 Add cake to the menu
2c5e6aa First price list

$ git diff b81d307 4f2a9c1
--- a/prices.csv
+++ b/prices.csv
@@ -1,3 +1,3 @@
 item,price
-tea,2.50
+tea,2.80
 cake,3.20

A branch keeps work apart

Every repository starts with one branch, often called main. The live website is usually built from it.

To try a change safely, you make a new branch. Your commits go on it, and main stays as it was.

When the change is ready, the team joins it back into main. You learn that step in a later module.

The commands you use every day

CommandWhat it does
git initStarts a new repository in this folder
git statusShows what changed and what is staged
git add fileStages a change
git commit -m "why"Saves the staged changes as a commit
git log --onelineLists past commits
git diffShows the lines that changed
git switch -c nameMakes a new branch and moves to it

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

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