Sub topic 12
Keep notebooks manageable in Git
This chapter assumes you can use basic Git commands (add, commit, diff) and have a project with a virtual environment and a working notebook. It covers why notebooks are awkward in Git and how to reduce the pain.
Why notebooks make noisy diffs
A .ipynb file is JSON. It stores your code, your Markdown, and every cell output. Outputs include tables, error traces, and plots encoded as base64 text. Re-running one cell can change thousands of lines in the file, even if your code did not change.
This causes three common problems:
- Diffs show output changes that bury the real code changes.
- Repositories grow quickly when large plots are committed again and again.
- Merge conflicts appear in JSON metadata, such as execution counts, that nobody edited on purpose.
The usual fix is to commit the code and leave the outputs out.
Strip outputs with nbstripout
nbstripout installs a Git filter. When you run git add or git commit, the filter removes outputs from the version Git stores. The file on your disk is not changed, so you still see your plots.
Run these commands inside the Git repository, with your virtual environment active:
python -m pip install nbstripout
nbstripout --installThe --install option writes the filter into this repository's Git configuration. Each person who clones the repository must run it once on their own copy, because filter settings in .git/config are not shared.
To check that it works, edit a cell, re-run it, and run git diff. You should see only the code change.
Ignore files that do not belong in Git
Add a .gitignore file so generated files stay out of the repository:
.venv/
.ipynb_checkpoints/
__pycache__/The .venv/ folder holds installed packages. It is large and tied to your machine, so share requirements.txt instead. Jupyter writes autosave copies into .ipynb_checkpoints/, and these only add clutter.
Large data files are a separate issue. If data/sales.csv is big or private, keep it out of Git and document where to get it.
Commit a clean, ordered notebook
Before you commit, restart the kernel and run all cells from the top. This confirms the notebook works in order and avoids committing code that depends on hidden state. Then commit:
git add analysis.ipynb requirements.txt .gitignore
git commit -m "Add sales revenue analysis"Common errors
| Problem | Likely cause | Fix |
|---|---|---|
| Outputs still appear in diffs | `nbstripout --install` was not run in this clone | Run it again in the repository |
| `nbstripout: command not found` | The virtual environment is not active | Activate `.venv`, then retry |
| Teammates see no plots after cloning | Outputs were stripped on purpose | Run the notebook, or share an HTML export |
| Merge conflict in a `.ipynb` file | Two people edited the same cells or metadata | Resolve it in the JSON, or take one version and re-apply the other's changes |
Exercise
You ran nbstripout --install in your repository. A teammate clones it and still sees large output changes in their diffs. What is the most likely cause, and what should they do?
Show answer
The filter is stored in each clone's local Git configuration, not in the shared repository. The teammate never installed it in their copy. They should activate the virtual environment, install nbstripout, and run nbstripout --install inside their clone.
Exercise
Why should you restart the kernel and run all cells before committing a notebook?
Show answer
It proves the notebook runs correctly from top to bottom with no hidden state from earlier or deleted cells, so the committed code works for the next person.