Skip to content

Notebooks in git

jOtter writes standard nbformat 4 JSON with the same layout as JupyterLab, so switching tools doesn’t cause noisy diffs. Its autosaves live in ~/.local/state/jotter/, never in your repository.

If your repository keeps notebooks as scripts, so that outputs never reach git, jOtter opens and saves them directly: jotter analysis.py.

  • A .py in jupytext’s percent format, with # %% cell markers as VS Code, Spyder and PyCharm write them, opens with its cells. # %% [markdown] blocks are markdown cells. Magics such as %matplotlib inline are commented out in the file and uncommented in the cell, as jupytext does.
  • Any other .py opens as one code cell, and saves as a plain script again as long as it stays one cell.

A .py can’t hold outputs, execution counts or cell ids. Outputs stay in memory while jOtter is open: running cells doesn’t mark the notebook as modified, q quits without asking, and the autosave keeps them for crash recovery. To keep outputs, save as a notebook with :w analysis.ipynb; :w analysis.py converts the other way.

A file jupytext wrote is saved back byte for byte, with its header (the kernelspec and jupytext’s pairing settings) untouched, so a paired setup (jupytext --set-formats ipynb,py:percent) sees only the lines you changed. jOtter doesn’t add a header to a file that has none; the kernel then comes from --kernel or the usual lookup.

Not supported: jupytext’s other formats (light, Rmd, MyST) and marimo notebooks. Convert them with jupytext --to py:percent.

Outputs, especially images, make diffs large. Press C to clear all outputs, then w to save. u brings them back if you change your mind.

To strip outputs automatically on every commit, use nbstripout:

Terminal window
pip install nbstripout
nbstripout --install # in the repository; adds a git filter

Your working copy keeps its outputs; only the committed version is stripped.

nbdime shows notebook diffs cell by cell instead of as raw JSON:

Terminal window
pip install nbdime
nbdime config-git --enable
git diff notebook.ipynb

When git changes a notebook that’s open in jOtter, jOtter reloads it, or asks first if you have unsaved changes. See Data safety.