Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 10: Version control with Git and GitHub

Objectives: LO-10.1, LO-10.2, LO-10.3, LO-10.4
Prerequisites: the Module 10 reading, Lab 9, and Git installed, as Setting up your computer describes
Time: about 2 hours

In this lab you build the history of a small project, a shopping list script, from its first commit to a merged branch: you commit, predict what git status reports, read the history, ignore generated files, write a commit message and a README, push to a remote, undo mistakes, and merge a branch. As before, each exercise ends with a check cell that prints passed or says what is wrong, and fails until you answer.

Each section builds on the one before it, so run the cells in order. If the repository gets into a state you can’t fix, run the setup cell again, which deletes it and starts over, and then run each cell after it again in order.

Setup

Run the next cell first. It prints the versions of Python and Git, deletes anything an earlier run left in scratch, and makes the folder scratch/lab10 with the project’s first files. It also defines a helper, git, which runs a Git command in that folder and prints what Git prints. Type the command as you would after git in a terminal: git("status") runs git status, and git('commit -m "Add a list"') runs git commit -m "Add a list". Put single quotes around a command that has double quotes in it, as that one does.

The helper changes three things from a terminal, so that the lab works the same on every computer:

  • It never opens an editor. Give every commit a message with -m; a merge or a revert uses the message Git writes for it.

  • It records commits as “PY101 Learner”, not with the name and address you set with git config.

  • It only finds repositories inside scratch, so a mistake here can’t reach any other repository on your computer.

1. A repository and its first commit

scratch/lab10 holds two files: shopping.py, the program, and notes.txt, your own notes, which don’t belong in the project. Run the next cell. Git can’t report anything yet, because the folder isn’t a repository, and the command fails with exit status 128.

Exercise L10.1 (LO-10.1, ★, about 10 minutes)

In the next cell, make the folder a repository, and record shopping.py, and only shopping.py, in its first commit, with the message Add a shopping list script. Use one git(...) line for each command, and add git("status") lines between them to watch what each one does.

Hint

Three commands: init, then add with the file’s name, then commit with -m and the message in double quotes.

2. What Git reports

Git reports each file in one of four states, which this lab names like this:

  • "untracked": in the folder, but never added;

  • "modified": changed since the last commit, and not staged;

  • "staged": its changes are in the staging area, ready for the next commit;

  • "unchanged": the same as in the last commit (git status doesn’t list it).

Exercise L10.2 (LO-10.1, ★★, about 10 minutes)

The cell after the check takes these four steps, in order:

  1. It adds "apples" to ITEMS in shopping.py, and saves the file.

  2. It runs git add shopping.py.

  3. It adds "rice" to ITEMS, and saves the file again.

  4. It creates a new file, prices.txt.

Without running it, predict:

  1. s1: the state of shopping.py after step 1.

  2. s2: the state of shopping.py after step 2.

  3. s3: after step 3, a git commit would record shopping.py with which new items? Answer "apples" or "apples and rice".

  4. s4: the state of prices.txt after step 4.

Now run the next cell, which takes the four steps and shows git status --short after each. --short prints one line for each file Git lists, with two columns of codes before the name: the first column for the staging area and the second for the working tree. M means modified, and ?? means untracked. After step 3, MM says that shopping.py has a staged change and another change that isn’t staged.

3. Reading the history

Run the next cell. It commits both changes to shopping.py, then makes three more commits, as someone else working on the project might have.

Exercise L10.3 (LO-10.1, ★★, about 10 minutes)

Use git log --oneline, git show with a commit’s hash, and git show HEAD (HEAD is the latest commit on your branch) in the next cell to find:

  1. h1: how many commits the repository has.

  2. h2: the item that the commit “Remove an item that is already at home” removed, as a string such as "milk".

  3. h3: how many lines the latest commit added, and h4: how many it removed.

4. Ignoring files

Run the next cell. It makes the kinds of files that collect in a project folder: Python’s cache, a virtual environment, and two logs, one of them in a folder. (They are stand-ins, with almost nothing in them.) --untracked-files=all makes git status list every file inside an untracked folder, not only the folder.

Exercise L10.4 (LO-10.2, ★★, about 15 minutes)

  1. In the first cell below, write the repository’s .gitignore, so that Git ignores every __pycache__ folder, the .venv folder, every file whose name ends in .log, wherever it is, and notes.txt. Running the cell saves the file.

  2. In the second cell, stage .gitignore, and commit it with a message of your own.

When you are done, git status reports nothing to commit.

Hint

Four lines: a folder’s name ends with /, and * matches any characters. git check-ignore -v followed by a path tells you which line ignores it.

5. A commit message

The list’s last line says You need 1 items. when the list has one item. The cell after the check fixes that line, so it says You need 1 item. for one item and You need 4 items. for four, and commits the fix with the message you write here.

Exercise L10.5 (LO-10.2, ★★, about 10 minutes)

Write the commit’s message as two strings, following the conventions in the reading:

  • summary: what the commit does, as an instruction (“Fix …”, “Say …”), in 50 characters or fewer, without a period at the end;

  • body: a sentence or two on why the change was needed, which the summary doesn’t say.

Run the next cell. It makes the change, commits it with your message, and shows the commit. Git puts a blank line between the summary and the body, as it does for two -m options in a terminal.

6. A README

Run the next cell to see what the program prints now. It runs shopping.py from the repository’s folder, as python shopping.py would in a terminal there.

Exercise L10.6 (LO-10.3, ★★, about 15 minutes)

  1. In the first cell below, write a README.md for the project that tells a stranger what it does and how to run it. It needs:

    • a # title on its first line;

    • a sentence that says what the program does;

    • a ## section whose heading has the word “run” in it, such as ## How to run, with the command python shopping.py in a fenced code block, and then a second fenced block with what it prints, copied from the cell above.

  2. In the second cell, stage and commit it.

Markdown needs a blank line before each heading’s text, each list, and each fenced block.

7. Pushing to a remote

The setup cell made scratch/lab10-remote.git, a bare repository: one with a history but no working tree, which is the kind GitHub keeps. It stands in for GitHub here, so you can push without an account or a network connection; the commands are the same. Its address, from the project’s folder, is the relative path ../lab10-remote.git.

Exercise L10.7 (LO-10.3, ★, about 10 minutes)

In the next cell, add the bare repository as a remote named origin, and push main to it, so that later pushes need no names. Then list the remotes, to check the address.

Run the next cell. It clones the remote, as a stranger would clone your repository from GitHub, and lists what they get: every committed file, and none of the ignored ones.

8. Undoing mistakes

Run the next cell. It makes two mistakes: it saves shopping.py with almost everything in it deleted, and it replaces the README with the word “TODO” and stages that.

Exercise L10.8 (LO-10.4, ★★, about 10 minutes)

Neither mistake was committed. In the next cell, put both files back as they are in the last commit. Read the hints that git status printed above.

Run the next cell. It commits a change that lists the items from Z to A, and pushes it, before anyone notices that the list is harder to use that way.

Exercise L10.9 (LO-10.4, ★★, about 10 minutes)

The commit is pushed, so undo it with a new commit that reverses it, using Git’s message for that commit, and push the result.

9. A branch

The next change gives each item a quantity: two eggs, six apples. You’ll make it on a branch, so that main keeps working until the change is finished.

Exercise L10.10 (LO-10.4, ★★★, about 15 minutes)

This exercise has three cells for your commands, with a cell of ours between each two.

  1. In the first cell, create a branch named quantities and switch to it. The cell after it writes the new shopping.py.

  2. In the second, commit the change on your branch, with a message of your own, and switch back to main. The cell after it makes a commit on main that adds a line to the README, so the two branches have each moved on.

  3. In the third, merge quantities into main, and delete the branch, which is no longer needed.

Run the next cell to see the shape of the history: the branch’s commit and the README’s commit, joined by the merge commit at the top.

Summary

In this lab you:

  • made a repository, and chose what went into its first commit with git add;

  • predicted whether a file was untracked, modified, or staged, and saw that a commit records the staging area;

  • read a project’s history with git log and git show;

  • ignored generated files and personal notes with .gitignore;

  • wrote a commit message with a summary and a body;

  • wrote a README that tells a stranger what the project does and how to run it;

  • pushed to a remote, and cloned it as a stranger would;

  • restored a file, unstaged a change, and reverted a pushed commit; and

  • made a change on a branch and merged it into main.

Next, the Module 10 practice has you do the same in a real terminal, and publish a project on GitHub.