The mental model behind commits, branches, and merges
Every result you trust rests on one question: exactly which version of the code and data produced it?
Git is the machine that gives that question an answer.
For a data scientist, these three add up to one word: reproducibility.
The unit of git. One commit records three things.
Each commit points back to the one before it. That is the whole history.
Each commit’s ID is computed from its contents. Change the past and every ID downstream changes with it.
That is how git guarantees the history has not been quietly tampered with.
Many of the diagrams from here on are a photograph of a real git repository. A command is run, and git’s actual state is read back and drawn.
A commit is a circle. An arrow runs to its parent. A colored box is a branch (just a pointer). HEAD marks where you stand. An amber ring is the commit the last command just made.
Use the arrow keys. Each step is one command, and the graph grows as you go.
You edit, you choose what to record, then you record it.
Clone a project and you copy the entire history to your machine, not just the latest files.
A branch is a separate line of work: a movable pointer that lets you build without disturbing the main line.
The mechanics of branching are settled. What teams argue about is the workflow: when to branch, how long a branch lives, when to merge.
Where a branch is reviewed, tested, and let into main.
Small, frequent merges beat one big one. This is the logic behind trunk-based development and feature flags.
The workflow is not the mechanics. It is the set of rules a team agrees on: when to branch, how long a branch lives, when to merge. Four common shapes, each on a real repository.
Git rarely loses committed work. A commit that is no longer on any branch still sits in the object store, and the reflog remembers every place HEAD has been. The next diagrams show the real safety net: reset, revert, restore, cherry-pick, and the reflog that recovers a “lost” commit.
Notebooks and large data files fight git’s line-by-line model. Parallel work, by people or by coding agents, needs its own hygiene: worktrees for isolation, and care with a shared checkout.
A chain of snapshots,
each pointing at its parent.
Everything else, branches, merges, rebases, remotes, is bookkeeping on top of that.
Built from the version-control notes in a software-engineering-basics collection.