Sub topic 11
Keeping notebooks reviewable in Git
This chapter assumes you can use Git for commits, diffs, and pull requests, and that you can run a Jupyter notebook from the command line. You should also have a project layout like the one from the earlier chapter, with notebooks in their own folder.
Why notebook diffs are hard to read
A .ipynb file is JSON. It stores your code, but it also stores cell outputs, execution counts, and sometimes embedded images as base64 text. Re-running one cell can change hundreds of lines in the file.
Three things go wrong as a result:
- Reviewers cannot find the real change in the noise.
- Merge conflicts appear in output blobs that nobody wrote by hand.
- Outputs can leak data, such as printed rows from a private table.
The goal is that a diff shows only what a person changed: the code and the text.
Strip outputs on commit
nbstripout installs a Git filter. The filter removes outputs and execution counts from the version Git stores. Your working copy keeps its outputs, so nothing disappears while you work.
pip install nbstripout
nbstripout --installRun this once in each repository. The filter settings live in .git/config, so they are not shared through the repository. Each collaborator must run the command after cloning. The --install command also writes a filter=nbstripout line to .git/info/attributes.
What you give up, and what to do about it
Stripped notebooks have no results when someone views them on a Git host. You have two options for sharing results:
- Save an executed copy as a build artifact, using the commands from the chapter on running notebooks from the command line.
- Export to HTML or PDF and publish that, outside the source tree.
Keep the source notebook clean and treat executed copies as outputs, in the outputs/ folder from your layout.
A review workflow
The following flow keeps changes small and reviewable.
Follow these habits:
- Restart the kernel and run all cells before you commit, as in the earlier clean-run check. Stripping outputs hides stale state, so you cannot see it in the diff.
- Make one logical change per commit.
- Move reusable code into modules. Changes in
.pyfiles give ordinary line-by-line diffs. - Run
git diffand check that only intended lines changed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Diffs still show outputs | Filter not installed in this clone | Run `nbstripout --install` |
| Notebook shows as modified right after checkout | Filter was installed after the file was committed with outputs | Run `nbstripout notebook.ipynb`, then commit the stripped file |
| Conflict inside a `metadata` block | Two people have different kernel or version metadata | Resolve by keeping one value, then check that the JSON is valid |
| Large repository history | Images were committed before stripping was set up | Strip going forward. Rewriting history is a separate decision to discuss with your team. |
Exercise
You installed nbstripout, edited a notebook, and ran every cell. A colleague pulls your commit and sees no outputs in the notebook. Is something broken, and how can they see the results?
Show answer
Nothing is broken. The filter removed outputs from the committed version, as intended. The colleague can run the notebook themselves, or open the executed copy or HTML export that you published as a build artifact.