Version control for data projects
Last updated
Git keeps every version of a project, so you can see what changed, when and why, and go back. It was built for code, and notebooks and data need a little extra care. In this lesson you'll decide what belongs in a data project's repository and what doesn't, clear outputs before committing, and read notebook changes with nbdime instead of raw JSON.
By the end of this lesson you can
- Decide which files of a data project to commit, and which to ignore
- Clear notebook outputs before committing
- Compare notebook versions with nbdime
Git, briefly
Git records snapshots of a project folder, called commits, each with a message saying what changed and why. It lets you see exactly what changed between any two versions, go back to an old one, and work with others without emailing files.
If you haven't used git, Simple Git Stage 1 teaches it from the start: install, git init, git add, git commit, git log and .gitignore. This lesson assumes those, and adds what data projects need.
Wrong way: the 200 MB file and the key
Ben set up git for the Tindahan project with git add . and committed everything. The push to GitHub was refused: one file was over 100 MB. While he was deleting it, he saw that .env, with the weather service's API key, was in the commit too. Deleting a file in a new commit doesn't remove it from the old ones.
Sort the project's files:
0 of 8 placed right
analysis.ipynb, with outputs cleared
clean.py
requirements.txt and README.md
data/raw/receipts_2024.csv, 200 MB
.env, with the weather API key
data/raw/cards.csv: card holders’ barangays and spending
reports/2024-12.html
.venv/
Then write a .gitignore before the first commit, so git add . can't pick them up:
# .gitignore for a data project .venv/ .env .ipynb_checkpoints/ __pycache__/ data/raw/ data/clean/ reports/
If a key does get committed, treat it as public: cancel it and make a new one with the service, then remove it from the history (Simple Git 7.6 shows how).
The check: run git status before every commit and read the list. Anything big, secret or personal shouldn't be on it.
Notebooks in git
A notebook file is JSON: the code, the Markdown, and every output: tables as HTML, charts as long strings of image data, and the In [14] counters. Rerun a notebook without changing a line, and git sees hundreds of changed lines.
Most data teams clear the outputs before committing. The notebook keeps its code and Markdown, and the outputs come back when it's rerun (10.4 makes sure they come back the same):
(.venv) $ jupyter nbconvert --clear-output --inplace analysis.ipynb (.venv) $ git add analysis.ipynb (.venv) $ git commit -m "Title the Rosario chart with its finding"
Commits then show only real changes, the repository stays small, and nothing from the data (a table of customers in an output, say) slips into git by accident. The rendered report (10.2) is where people read the outputs.
Reading notebook changes
Even with outputs, a notebook change should be readable. One chart title was changed, and the notebook rerun. Compare the two ways of seeing that change:
@@ -212,7 +212,7 @@ "cell_type": "code", - "execution_count": 14, + "execution_count": 6, "metadata": {}, @@ -221,11 +221,11 @@ - "image/png": "iVBORw0KGgoAAAANSUhEUgAAA+gAAAH0CAYAAADL1t+KAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZl… + "image/png": "iVBORw0KGgoAAAANSUhEUgAAA+gAAAH0CAYAAAC8n1b2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZl… @@ -240,9 +240,9 @@ - "ax.set(title=\"per_day yoy by store\")\n", + "ax.set(title=\"Rosario is the only store below last year\")\n", @@ -301,7 +301,7 @@ - "execution_count": 15, + "execution_count": 7,
Four places changed, one of them a line of image data hundreds of thousands of characters long. Which one was the actual edit? Only the third.
nbdime ("notebook diff and merge") understands notebooks: it compares cell by cell, shows code and outputs separately, and draws old and new charts side by side.
(.venv) $ python -m pip install nbdime (.venv) $ nbdiff analysis.ipynb # against the last commit (.venv) $ nbdiff-web analysis.ipynb # the same, in the browser (.venv) $ nbdime config-git --enable # git diff uses nbdime for notebooks
Two notebooks changed by two people at once can be merged with nbdime too (nbmerge), cell by cell, where git's line-by-line merge would break the JSON.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Pro Git, 2nd ed., section 2.2: Recording changes to the repository: staging, committing, and .gitignore
- Git: gitignore documentation: patterns for files git should ignore
- nbdime: Console commands: nbdiff, nbdiff-web, and the -s and -o filters
- nbdime: Version control integration: making git diff use nbdime for notebooks
- nbconvert: Configuration options: --clear-output and --inplace