---
title: Git-Friendly Jupyter
description: Configure Git diff/merge drivers and optional Jupyter pre-save cleaning
order: 4
---
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`:

```sh
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`):

```text
*.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.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## 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:

```python
%%sh
pwd
```

```output
/private/var/folders/ft/0gnvc3ts5jz4ddqtttp6tjvm0000gn/T/tmpez8nec5v/repo
```

```python
%%sh
git init
```

```output
Initialized empty Git repository in /private/var/folders/ft/0gnvc3ts5jz4ddqtttp6tjvm0000gn/T/tmpez8nec5v/repo/.git/
```

```python
%%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
```

```output
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.

<img src="images/jupyter-unreadable-notebook.png" alt="Jupyter notebook shows the above error when opening a notebook with merge conflicts." fig-alt="Jupyter notebook error: 'Error loading notebook: Unreadable Notebook...'" width="800px" fig-align="center" />


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:

<img src="images/jupyter-friendly-conflict.png" alt="" fig-alt="Jupyter-friendly git conflict with patches in individual cells separated by conflict markers" width="400px" fig-align="center" />

### `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:

```diff
  {
   "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:

```diff
-<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:

```json
{
  "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](pre_commit.md). 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`:

```python
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`:

- `jupyter_hooks = true`: enable opt-in Jupyter pre-save cleaning for this project (default `false`).
- `clear_all = true`: remove all cell outputs and non-fastship metadata on clean.
- `allowed_metadata_keys` / `allowed_cell_metadata_keys`: preserve specific metadata keys.
- `clean_ids = true`: strip memory addresses from default Python reprs (default `true`).
- `to_meta = ["export"]`: move listed directives from comments to cell metadata on clean.
