Track changes and dependencies
This is a useful moment to protect the work you can now explain. In the previous lesson, moving one line changed a lookup's result. When an AI edits a larger file, you need a clear view of that change and the environment used to test it.
This root lesson has two Parts. Git records and displays changes. uv manages the Python environment and its dependencies. They answer different questions, and you will use both when reviewing work on a real solution.
By the end, you should be able to:
- Explain the lines included in a staged diff.
- Identify the environment and dependencies used for a run.
- Recognise what a clean diff or passing run still leaves unproved.
Part 1: Look at the change before saving its history
Git is version-control software: it records selected file states so you can inspect how they change over time. It runs on your computer. GitHub is a separate hosting service; you do not need to upload anything for this lesson.
First, confirm Git is available:
git --version
If it is missing, use the official Git installation guidance. Then open a new terminal and check again.
Use your own book-work folder from the terminal lesson. A repository is the set of files Git tracks together with its history. If the folder already belongs to one, keep it. If it is a new independent folder, git init starts its history. Do not initialise a second repository inside an existing one just because an AI suggested the command.
These three views are worth understanding:
git status
git diff
git diff --staged
status tells you which files are changed, staged or untracked. An untracked file is one Git has not been asked to record. diff shows tracked changes that are not staged. diff --staged shows the changes selected for the next commit.
A commit records the selected state in local history. The staging area is the selection for that next commit. Saving a file in an editor and staging it in Git are separate actions.
In a diff, the --- and +++ header lines identify the old and new filenames. Inside the change sections, a line beginning with - was removed and a line beginning with + was added. Those signs belong to the diff display; they are not characters to paste into Python. Compare an actual change to your saved chapter_labels.py with the behaviour you intended. The earlier lookup example showed how moving a return could change an answer; a small diff can contain a large behaviour change.
Select an actual reviewed file by name:
git add -- chapter_labels.py
git diff --staged
git commit -m "Add the chapter label function"
The -- separates options from the filename. Review the staged diff before committing, because git commit records what is staged, including anything staged earlier. Do not replace the named selection with “add everything” unless you have inspected every included file.
Git may ask for an author name and email before the first commit. Use values you choose for that repository; an AI must not invent an identity for you. A commit does not upload files. A push is a later, separate action.
On Windows, Git may also warn about converting LF and CRLF, two line-ending formats. That warning is not itself a failed commit. Inspect the command's outcome and diff before changing settings or asking the AI to rewrite code.
Keep secrets and generated files out of the selection
A .gitignore file names files Git should normally leave untracked. For this Python work, examples include:
.venv/
__pycache__/
.env
.pytest_cache/
.venv will hold an environment, and .env may hold local settings or secrets. You will meet configuration in the next root lesson. Add ignore rules before staging those files.
Ignoring a file does not remove it if it is already tracked. It also does not erase a secret from an older commit. If a credential has entered history, treat it as exposed and replace it through the service that issued it. A later deletion is not enough.
Part 2: Run with the dependencies you intended
Python includes a standard library, modules supplied with the interpreter. Other libraries must be installed. A dependency is software your code relies on. The test runner pytest is one dependency we will use in the chapter's final lesson.
Installing packages into whichever Python happens to be on your command path can mix unrelated work. A virtual environment gives one solution its own installed package set. It does not create a separate computer or prevent the code from reading your files.
uv can create and maintain that environment. Follow its official installation instructions for your operating system, then check:
uv --version
Inspect installation instructions before running them. A command that downloads and executes a script deserves the same review as any other executable code.
Inside your independent book-work folder, initialise the Python package information if it does not already exist:
uv init --vcs none
--vcs none prevents uv from starting another version-control repository; you already made that Git decision explicitly. The command creates Python metadata and starter files, so inspect the new file list with git status. If the folder already has pyproject.toml, inspect and use that file rather than replacing it. pyproject.toml records information about the Python work and its dependencies. uv.lock records the resolved package versions. .venv contains the installed environment and can be rebuilt.
Now add the test runner for development:
uv add --dev pytest
uv run python chapter_labels.py
--dev records a development dependency. uv run prepares the managed environment and runs the command inside it. This avoids relying on a separate activation step. The chapter label should be the same as before; changing how you select an environment should not change the function's intended result.
For shared work, normally record pyproject.toml and uv.lock in Git, and leave .venv untracked. Review the dependency change and its source before accepting it. A lockfile records versions; it does not prove every package is trustworthy or that every operating system behaves identically.
Safety floor
Review the exact files in a commit and the exact packages in an installation. Keep credentials out of code, history and shared logs. An isolated Python environment separates dependencies; it is not a security boundary around untrusted code.
The Git diff reference and uv guide to managed Python work explain the commands used here.
Check both kinds of evidence
Answer before opening the comparison:
- You saved a file after staging it. Which command shows whether the staged version differs from your latest edit?
- Why does adding
.envto.gitignorefail to repair an already published secret? - The code runs with
pythonbut fails withuv run python. What difference would you investigate first? - Why do you record the lockfile but usually rebuild the environment folder?
Compare after your attempt
git diff shows unstaged edits; compare that with git diff --staged before the commit. Ignore rules neither remove existing history nor revoke a credential. Investigate which interpreter and installed dependencies each command selects, rather than immediately rewriting the function. The lockfile describes the resolved versions; the environment contains installed files that can be recreated for the machine.
Ask the AI to explain one actual diff in your own authorised solution. Specify the behaviour that must remain unchanged, and check that its explanation accounts for every changed line. Then continue to functions and error boundaries, where we make a function's responsibility more precise.