Learn Git By Doing: type a command, watch the graph change
How we built an open-source Git trainer: a simulated terminal, a pure-function model of a repository, and a D3 graph that redraws after every command you type.
- Published
- Reading time
- 5 min
- Project
- Learn Git By Doing
- Topics
-
- React
- TypeScript
- D3.js
- Zustand
- Vite
Most people learn Git by memorising commands and hoping nothing breaks. The commands are easy to type and hard to picture, because the thing they change, a graph of commits and the labels pointing into it, is invisible in a normal terminal. Learn Git By Doing puts that graph on screen. You type real Git commands into a simulated terminal in the browser, and a D3 visualisation of the working directory, the staging area, the commits and the branch labels redraws after each one. Nothing touches a real repository, so you can try reset --hard without fear. It is free and open source at github.com/kiridharan/learngitbydoing, and it is a prototype: the core loop works, and several parts of Git are not modelled yet. We will be specific about both.
A repository as plain data
The first decision was not to run Git at all. Compiling Git to WebAssembly or emulating a file system would be faithful and heavy, and most of its behaviour would never be visible to a learner. Instead, the repository is a small typed object: whether it is initialised, the files in the working directory, the files in the staging area, the list of commits with their parents, the branches with the commit each points to, the current branch, and HEAD.
A commit stores its parents as an array, so a merge commit simply has two. That one choice is what lets the graph show a merge as two lines meeting, instead of pretending history is a straight line.
Every command is a pure function
Each Git command is a function that takes the current state and returns a result: whether it succeeded, the lines to print in the terminal, and, if something changed, the new state.
interface CommandResult {
success: boolean;
message: string;
output: TerminalLine[]; // what the terminal prints
stateChange?: Partial<GitState>;
}
The functions never mutate anything. A small React hook parses the input, calls the right function, appends the output to the terminal history, and applies the new state only when the command succeeded. A failed command prints its error and leaves the repository exactly as it was, which is also how real Git behaves and is worth learning early.
This shape made the project easy to extend. Adding merge and reset meant writing two more functions and two more cases in the hook, with no change to the terminal or the graph. It also means each command can be tested by handing it a state and checking what comes back, without a browser.
The functions copy Git’s guard rails as well as its happy paths. git commit with nothing staged prints “nothing to commit”. git merge refuses to merge a branch into itself, refuses when there are uncommitted changes, and says “Already up to date” when both branches point at the same commit. Those messages are where learners usually get stuck, so the simulator reproduces them rather than hiding them.
Drawing the graph with D3
The visualisation is plain D3 on an SVG, redrawn whenever the repository state changes. It shows the working directory and staging area as separate areas, so git add visibly moves a file from one to the other, then the commits as nodes joined to their parents, branch labels pointing at their commits, and a marker for the current branch. React owns the state; D3 owns the drawing. Keeping those apart avoids the common trap of two libraries fighting over the same DOM nodes.
Lesson progress is separate from the repository. It lives in a Zustand store persisted to the browser’s local storage, so completed lessons survive a reload. The store keeps a hash of the lesson list. When we add or rename lessons, it merges the new list with the learner’s saved progress instead of wiping it, a small detail that matters once real people have used a course.
The lessons are grouped into three modules: Git fundamentals, branching, and undoing changes.
What is built, and what is not
The simulator supports init, add (named files or .), commit -m, status, log, branch, checkout, merge, reset to unstage, reset --hard to a commit, and help. init seeds two sample files so there is something to stage straight away.
Several things are not modelled yet. You cannot create or edit files from the terminal. Merges always create a merge commit, so fast-forward merges are not shown. Conflicts are explained in a lesson but not simulated. checkout -b is described in the lessons but not yet accepted by the parser. There are no remotes, so push and pull cannot be practised. The graph lays commits out in a single row rather than a lane per branch, which gets crowded on longer histories. Each of these is a contained change because of the pure-function design, and they are the next pieces of work.
What we’d tell a team building something similar
Model the concept, not the tool. A learner needs the parts of Git they can see, and a small typed state is easier to build, test and explain than an emulator.
Make every action a pure function from state to state. It keeps the UI simple, makes failures safe, and turns each new feature into one function and one test.
Be explicit about gaps in a teaching tool. A simulator that quietly behaves differently from the real thing teaches the wrong lesson.
If you’re planning an interactive learning product, get in touch.