Skip to content

0.7 Git I: saving your work with version control

IGCSE and A LevelPart 0

Suggested time: 1 lesson · You need: Chapters 0.3 and 0.4

What you will do

Use Git to take snapshots of your project, see what changed, and go back to an earlier version when something breaks. You will use it from the Castle Adventure onwards, committing after every step. By the end of this chapter you will be able to:

  • explain what version control is and why programmers use it;
  • turn a folder into a Git repository;
  • save snapshots with add and commit;
  • see the history with log and changes with diff;
  • undo changes and look at, or restore, an earlier version;
  • tell Git which files to ignore.

Key vocabulary

Term Meaning Example
Version control Keeping a history of every saved version of a project Git
Repository (repo) A project folder whose history Git is tracking CastleAdventure/
Commit A snapshot of the project, with a message saying what changed Ask for the player's name
Staging Choosing which changes go into the next commit git add main.py
Commit hash The unique ID of a commit (a short form is shown) b754942
.gitignore A file listing things Git should not track .venv/

The big idea: snapshots you can go back to

Saving a file overwrites the old version. Copying files to main_v2.py, main_final.py, main_final_REALLY.py gets messy fast. Git keeps every snapshot you ask it to, with a note of what changed and when, all inside a hidden .git folder in your project.

Concept: version control

How it works: you work normally, then stage the changes you want (git add) and commit them (git commit). Each commit is a complete snapshot with a message, a date and an ID. The commits form a history you can read and go back through.
Why we use it: you can experiment without fear, because you can always return to the last working version. The history also shows how a program was developed, which is useful evidence for a programming project.
Where else it's used: almost all professional software is kept in Git, from small apps to operating systems. The book you are reading is too.

Step 1: Install Git and introduce yourself

Download Git for Windows from git-scm.com, or install it from the command line:

winget install --id Git.Git -e --source winget

Open a new terminal afterwards.

macOS: type git --version. If Git isn't installed, macOS offers to install its Command Line Developer Tools, which include Git. Or, with Homebrew: brew install git.

Linux (Debian/Ubuntu): sudo apt install git

Check it, then tell Git who you are. You only do this once per computer. Your name and email are written into every commit.

git --version
git config --global user.name "Alex Smith"
git config --global user.email "alex@example.com"
git config --global init.defaultBranch main

Which email?

If you will use GitHub (Chapter 0.8), use the same email as your GitHub account. GitHub can also give you a private "noreply" address to use instead, if you'd rather not publish your real one.

What's happening?

--global saves the setting for every project on this computer. The last line names the first branch main, which is the usual name today (Chapter 0.8 explains branches).

Step 2: Start a repository

Make a project folder with one file in it: main.py, containing print("Welcome to the Castle Adventure!"). Then:

cd CastleAdventure
git init
git status
Output
Initialized empty Git repository in …/CastleAdventure/.git/

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    main.py

nothing added to commit but untracked files present (use "git add" to track)

Run git init in the right folder

git init turns the current folder into a repository. Run it in your project folder, never in your home folder or on the whole Desktop. Check where you are first (cd on Windows, pwd on Mac/Linux).

Step 3: Your first commit

git add main.py
git commit -m "Title screen"
Output
[main (root-commit) ad65b4b] Title screen
 1 file changed, 1 insertion(+)
 create mode 100644 main.py

What's happening?

git add stages the file: it says "include this in the next snapshot". git commit takes the snapshot, and -m gives its message. ad65b4b is the start of the commit's unique ID; yours will be different.

Write messages that finish the sentence 'This commit will…'

Good: Ask for the player's name, Fix crash when the inventory is empty. Unhelpful: stuff, changes, asdf. A good history reads like a development diary.

Step 4: See what changed

Add two lines to main.py:

name = input("What is your name? ")
print("Welcome,", name)

Then ask Git what's different from the last commit:

git diff
Output
@@ -1 +1,3 @@
 print("Welcome to the Castle Adventure!")
+name = input("What is your name? ")
+print("Welcome,", name)

Lines starting + were added; lines starting - were removed. Commit the change and look at the history:

git add main.py
git commit -m "Ask for the player's name"
git log --oneline
Output
b754942 Ask for the player's name
ad65b4b Title screen

A short-cut

git commit -am "message" stages every file Git already tracks and commits in one go. New files still need git add first.

Step 5: Undo and go back

Throw away changes you haven't committed (for example, an experiment that went wrong):

git restore main.py

The file goes back to how it was at the last commit.

git restore can't be undone

Uncommitted changes that you restore are gone for good. Commit anything you might want later first.

Look at an older version without changing anything, using its ID from git log:

git show ad65b4b:main.py

Bring back an older version of a file:

git restore --source=ad65b4b main.py

main.py is now as it was in the Title screen commit, as a new, uncommitted change. Commit it if you want to keep it, or git restore main.py to change your mind.

Checkpoint

git log --oneline shows at least two commits, and you have used git diff and git restore.

Step 6: Tell Git what to ignore

Some files should never be in the history: files Python creates automatically, your virtual environment, and system clutter. Create a file called .gitignore in the project folder:

.gitignore
# Python creates these automatically
__pycache__/
*.pyc

# Virtual environments can be rebuilt from requirements.txt (Chapter 0.6)
.venv/
venv/

# Operating-system clutter
.DS_Store
Thumbs.db

Then commit it: git add .gitignore and git commit -m "Ignore generated files".

Git inside your editor

VS Code has Git built in: the Source Control panel shows changed files, and you can stage, commit and see differences by clicking. It runs the same Git commands for you. Learn the commands first, so you understand what the buttons do. Thonny has no Git support, so use the terminal alongside it.

The habit

Commit every time something works. In this book that means at least once per step of the Castle Adventure and once per stage of a game series. Small, frequent commits make it easy to find exactly when something broke.

Troubleshooting

What you see What to try
fatal: not a git repository You're not in the project folder, or you haven't run git init there.
Author identity unknown Run the two git config --global user… commands in Step 1.
nothing to commit, working tree clean There are no changes since the last commit. That's fine.
nothing added to commit but untracked files present You forgot git add.
A strange text editor opens when you commit You left out -m "message". In the editor Git usually opens (Vim), press Esc, type :q! and press Enter to cancel, then commit again with -m.

Check your understanding

  1. What is the difference between git add and git commit?
  2. Why is Fix crash when the inventory is empty a better commit message than changes?
  3. You broke main.py and haven't committed. Which command gets back the last committed version?
  4. Why should .venv/ be listed in .gitignore?
Answers
  1. git add chooses (stages) the changes to include; git commit saves them as a snapshot in the history.
  2. It says what the commit does and why, so you (or a teacher) can find it later and understand the history without opening the code.
  3. git restore main.py.
  4. It is large, is specific to one computer, and can be rebuilt from requirements.txt, so it doesn't belong in the history.

Summary

  • Git keeps snapshots (commits) of your project in a hidden .git folder.
  • git init once per project; then git add → git commit -m "…" every time something works.
  • git status, git diff and git log --oneline show where you are.
  • git restore throws away uncommitted changes; git restore --source=ID file brings back an older version.
  • List generated files and .venv/ in .gitignore.
This is a free sample: log in for every book →