Git-Friendly Jupyter

Configure Git diff/merge drivers and optional Jupyter pre-save cleaning

Jupyter notebooks are JSON files, so default git diff and git merge produce noisy diffs and broken JSON on conflict. fastship provides standalone Git diff and merge drivers (ship-diff-driver and ship-merge) plus optional notebook cleaning (ship-clean and a Jupyter pre-save hook). They work in any Git repository, even outside fastship projects.

Using the Git drivers does not require any Jupyter configuration.

Global Git setup (all repositories)

Install fastship, then register the merge and diff drivers once in ~/.gitconfig:

uv pip install -U fastship
git config --global merge.jupyternotebook.name "resolve conflicts with ship-fix"
git config --global merge.jupyternotebook.driver "ship-merge %O %A %B %P"
git config --global diff.jupyternotebook.command ship-diff-driver

Select which file patterns use the drivers in your global Git attributes file (default ~/.config/git/attributes, or check git config --global --get core.attributesFile):

*.ipynb merge=jupyternotebook
*.ipynb diff=jupyternotebook

ship-diff-driver handles both notebooks (cell-level diffs) and ordinary plain-text files (word-level diffs). To use it for text files too, register a driver (or reuse diff.jupyternotebook.command) and add patterns such as *.py diff=jupyternotebook or *.md diff=jupyternotebook to your attributes file.

Per-repository Git setup

To configure a single clone without editing global Git settings, commit .gitattributes in the repository and register the drivers with git config --local. Below we demonstrate this in a temporary directory:

%%sh
pwd
/private/var/folders/ft/0gnvc3ts5jz4ddqtttp6tjvm0000gn/T/tmpez8nec5v/repo
%%sh
git init
Initialized empty Git repository in /private/var/folders/ft/0gnvc3ts5jz4ddqtttp6tjvm0000gn/T/tmpez8nec5v/repo/.git/
%%sh
git config --local merge.jupyternotebook.name "resolve conflicts with ship-fix"
git config --local merge.jupyternotebook.driver "ship-merge %O %A %B %P"
git config --local diff.jupyternotebook.command ship-diff-driver
cat <<'EOF' > .gitattributes
*.ipynb merge=jupyternotebook
*.ipynb diff=jupyternotebook
EOF
git check-attr diff merge -- demo.ipynb
Hooks are installed.

Committing .gitattributes shares the file patterns with collaborators; each clone still opts into the local driver commands via git config --local (or once globally via git config --global). To remove local drivers later, run git config --local --remove-section merge.jupyternotebook and git config --local --remove-section diff.jupyternotebook.

What the drivers and cleaning tools do

fastship provides a merge driver (ship-merge), a diff driver (ship-diff-driver / ship-diff), and notebook cleaning (ship-clean).

ship-merge on merging notebooks with git

One of the biggest complaints when working with Jupyter is that merge conflicts break notebooks. This is particularly problematic in projects with many collaborators.

Jupyter notebook shows the above error when opening a notebook with merge conflicts.
Figure 1: Jupyter notebook shows the above error when opening a notebook with merge conflicts.

Oftentimes merge conflicts happen on metadata like cell execution counts. ship-merge automatically resolves conflicting outputs and metadata and leaves remaining source conflicts as valid notebook JSON that opens cleanly in Jupyter. It works in merge, pull, rebase, and stash. Run ship-fix notebook.ipynb to repair a notebook that already has raw git conflict markers.

Here's what the conflict looks like in Jupyter with fastship's merge driver:

ship-diff-driver and ship-diff on running git diff

Even with clean notebooks, plain git diff shows raw notebook JSON. Registering ship-diff-driver makes git diff display notebook changes cell by cell as concise source diffs, and also renders clean word-level diffs for ordinary text files when selected in .gitattributes.

You can also invoke the same renderer directly with ship-diff, which compares any two Git refs (or a ref and the working tree) for a notebook, directory, or whole project. fastship uses the same jupyternotebook driver name as nbdime, so they share .gitattributes lines and you can switch drivers at any time by updating git config.

Opt-in Jupyter pre-save cleaning

Jupyter notebooks store a variety of metadata (including execution counts and notebook extension info) that aren't conducive to collaborative version control systems like git. These pollute diffs in pull requests and git histories (which can make debugging harder), and tend to cause merge conflicts. For example:

  {
   "cell_type": "code",
-  "execution_count": 1,
+  "execution_count": 2,
   "metadata": {
     "hide_input": false
  }

Python's default repr is another example, since it includes a memory address which we usually aren't interested in:

-<matplotlib.axes._subplots.AxesSubplot at 0x7fbc11508950>
+<matplotlib.axes._subplots.AxesSubplot at 0x7fbc113dbe90>

Running ship-clean strips unwanted metadata, execution counts, and object memory addresses from default Python reprs, turning the examples above into:

{
  "cell_type": "code",
  "execution_count": null,
  "metadata": {}
}

and:

<matplotlib.axes._subplots.AxesSubplot>

You can run ship-clean manually, via ship-prepare, or via pre-commit hooks. If you use Jupyter Notebook or JupyterLab and want automatic cleaning on save, opt in by adding the following to ~/.jupyter/jupyter_notebook_config.py and/or ~/.jupyter/jupyter_server_config.py:

def fastship_clean_jupyter(**kwargs):
    try: from fastship.clean import clean_jupyter
    except ModuleNotFoundError: return
    clean_jupyter(**kwargs)

c.ContentsManager.pre_save_hook = fastship_clean_jupyter

Then enable it per project by setting jupyter_hooks = true in ship.toml or [tool.fastship] in pyproject.toml (defaults to false, so configuring the hook once in Jupyter will not modify notebooks in projects that have not opted in).

Customizing notebook cleaning

Configure notebook cleaning in ship.toml or [tool.fastship] in pyproject.toml: