Git for ML Projects

Versioning code and notebooks, branching for experiments and keeping large data and model files out of your repository.

Beginner · about 5 hours · difficulty 1/5

Every serious ML result should be traceable to a commit. Git gives you history, safe experimentation on branches and collaboration through pull requests.

Essentials

  • init, add, commit, log, diff, status as muscle memory.
  • Branches per experiment or feature, merging and resolving conflicts.
  • Remotes on GitHub or GitLab, pull requests and reviews.
  • A good .gitignore: data folders, checkpoints, .ipynb_checkpoints, environment folders.

ML-specific concerns

  • Do not commit large data or model weights. Use DVC, Git LFS or object storage instead (covered in MLOps).
  • Notebooks diff badly because outputs are stored as JSON. Clear outputs before committing, or use tools like nbstripout or Jupytext.
  • Record the commit hash with every experiment run so results are reproducible.

Good enough to move on when you can branch, commit, push, open a pull request and recover from an accidental commit without panic.

Key concepts

  • Commits, diffs and history
  • Branching and merging
  • GitHub remotes and pull requests
  • .gitignore for data projects
  • Notebook diffs with nbstripout or Jupytext
  • Git LFS

Mini project

Put your previous NumPy and pandas notebooks in a GitHub repo with a proper .gitignore, stripped notebook outputs, a README and one feature branch merged via pull request.

Check yourself

Why is committing a 2 GB CSV to Git a problem even if you delete it in the next commit?

Free resources