Skip to content
AI-grafen
DAI developerProgramming· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Git — version control

Be able to commit, branch, merge and read the history — and understand why experiments need version control.

Prerequisites

Intuition

Git saves snapshots of the whole project. Every commit is a state you can go back to, with a message that says why.

The three places a file can be:

working directory  --git add-->  staging  --git commit-->  history
   (your files)                 (what you                 (permanent)
                              mean to save)

The commands you actually use:

CommandDoes
git statuswhat has changed?
git add <file>add it to the next commit
git commit -m "..."save the snapshot
git log --onelineshow the history
git diffwhat differs?
git switch -c <name>a new branch
git merge <branch>merge it

A branch is a development line of its own. You can try something wild without breaking what works, and throw it away if it came to nothing.

Formal

Why ML work needs version control more than ordinary code does. A result is reproducible only if you know all the parts that produced it:

WhatHow it is versioned
CodeGit, as usual
Hyperparametersa config file in Git, not hard-coded in a notebook
Dataa hash or version number in Git; the data itself in DVC, LakeFS or an artefact store
Environmentrequirements.txt with pinned versions, or a container tag
Model weightsan artefact store, with a link to the commit
Random seedin the config

Saving the code but not the hyperparameters gives you a result you can never recreate. The rule: everything that affects the result must be identifiable by a commit id.

AI-grafen does exactly that — every lab run logs the commit, the seed, the input hash and the environment tag.

What should NOT live in Git:

NoWhyInstead
Secrets, API keysthe history is permanent — a deleted key is still thereVault, environment variables
Large data filesthe repo becomes unmanageableDVC, object storage
Model weightsthe samean artefact store
.ipynb_checkpoints, __pycache__noise in every diff.gitignore

The first line is important and underestimated: a key that has ever been committed must be treated as leaked, even if it was removed in a later commit. It is still in the history and in every clone. The right response is to rotate the key.

Merge conflicts arise when two branches have changed the same lines. Git marks them:

<<<<<<< HEAD
lr = 0.001
=======
lr = 0.01
>>>>>>> experiment

You edit the file into what you want, remove the markers, and git add + git commit. Conflicts are not an error — they are Git asking you to make a decision it cannot make for you.

Interactive

Do the whole flow in the terminal. Ten minutes, and you have tried everything that counts.

mkdir experiment && cd experiment && git init

# The first commit
echo "lr = 0.001" > config.py
git add config.py
git commit -m "Starting point: lr 0.001"

# Try something on a branch
git switch -c higher-lr
echo "lr = 0.01" > config.py
git commit -am "Try lr 0.01"

# Meanwhile the main branch changes
git switch main
echo "lr = 0.003" > config.py
git commit -am "Try lr 0.003"

# Merge → a conflict
git merge higher-lr
# CONFLICT (content): Merge conflict in config.py

cat config.py          # look at the markers
echo "lr = 0.01" > config.py     # make up your mind
git add config.py && git commit -m "Choose lr 0.01 after comparing"

git log --oneline --graph --all

Three commands that save you when something has gone wrong:

SituationCommand
Undo changes in a file (not committed)git restore <file>
Take a file out of staginggit restore --staged <file>
See what you did, even after a botched resetgit reflog

git reflog is the best news there is for someone who has just thought they lost their work: Git almost never forgets anything within a week.

Mastery means

  • Uses commit, branch, merge and log
  • Resolves a simple merge conflict
  • Explains why ML experiments need version control

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences