Sign inGet started
← Back to all guides

How to use Git with Jupyter / IPYNB

By Nick Barth

Updated on March 6, 2024

When working with data science projects, Jupyter notebooks are a popular tool among data scientists and researchers. Version control for these notebooks can be optimized by using Git, a widely used system for tracking changes in source code during software development. Here’s a guide on how to integrate Git with Jupyter/IPYNB effectively.

Getting started with Git

First, ensure you have Git installed on your system, which you can check by running `git --version` in your terminal. If it’s not installed, download and install Git from the official site or use platform-specific package managers like `apt` for Ubuntu or `brew` for macOS.

Initialize a Git repository

Navigate to the project directory in the terminal and initialize a new Git repository:

git init

This command will create a new `.git` folder, signaling that Git is now ready to start tracking changes.

Configure Git for Jupyter notebooks

Jupyter Notebooks are stored as JSON files and can sometimes create challenges when using Git due to the output cells which change frequently. To avoid tracking output changes and reduce merge conflicts, consider the following tools and practices:

Clear output before committing

Before committing changes, clear the outputs of all cells. You can do this in Jupyter by clicking on `Kernel` and choosing `Restart & Clear Output`.

Use .gitignore to exclude unnecessary files

Create a `.gitignore` file in the root of your project directory to exclude files or directories from being tracked by Git. Common entries for Jupyter projects include checkpoints and system-specific files:

.ipynb_checkpoints

.DS_Store

Git extensions for Jupyter notebooks

Extensions like `nbdime` provide tools for diffing and merging notebook files. Install `nbdime` via `pip` and integrate with Git:

pip install nbdime

nbdime config-git --enable --global

Regular Git workflow

Use the standard Git workflow when working with notebooks:

  • Stage changes: Use `git add <filename>` or `git add .` to stage your files.
  • Commit changes: Commit your changes with a message describing what was done using `git commit -m "Message"`.
  • Pushing changes: If you’re using a remote repository, push commits with `git push origin main` where `main` is the name of your branch.
  • Branching: For working on different features or experiments, create branches using `git branch <branch_name>` and switch between them with `git checkout <branch_name>`.

Collaboration and conflicts

When collaborating, you might face conflicts in your notebooks. This usually happens when the same cells have been edited by different people. To resolve conflicts:

  1. Open the notebook in Jupyter.
  2. Look for the `<<<<<<<`, `=======`, and `

Nick Barth

Product Engineer

Nick has been interested in data science ever since he recorded all his poops in spreadsheet, and found that on average, he pooped 1.41 times per day. When he isn't coding, or writing content, he spends his time enjoying various leisurely pursuits.

Follow Nick on LinkedIn and GitHub

That’s it, time to try Deepnote

Get started – it’s free
Book a demo

Footer

Solutions

  • Notebook
  • Data apps
  • Machine learning
  • Data teams

Product

Company

Comparisons

Resources

  • Privacy
  • Terms

© Deepnote