Skip to content
Antoine Le LouëtIndependent senior developer (Home)
Back to the Journal

Advice5 min read

Understanding Git: from a paper sketch to a merge

Drawing branches on paper helped me explain Git to junior developers. A simulator extends that approach to understanding merge, rebase and reset.

Before the merge, main points to B and atelier to C, with A as their common parent. After the merge, main points to M, whose parents are B and C. atelier stays on C.
A merge between two diverging histories creates commit M here. Lines connect each commit to its parents; labels show branch positions.

The moment we reached for a sheet of paper

For years, I taught Git to interns and junior developers. The first commands went smoothly, then the same questions returned: 'Which branch are we on now?' or 'Why did my commit disappear?' Explaining a merge in words often brought us back to the same place: a sheet of paper.

We drew circles for commits, branches and arrows. Looking at the drawing, we could follow what changed after a command. The discussion became more precise: which branch moves? Which commit is it attached to? What remains on the other side?

This difficulty in picturing history deserves attention. You can use Git daily with a handful of habitual commands, then hesitate as soon as branches diverge. To explain it, I prefer making visible what the command transforms.

Read the labels before following the lines

A commit records a project state and links to its parents. A branch is a movable reference to a commit. Creating a branch therefore adds a name pointing to an existing commit. The next commit advances the branch you are working on.

HEAD generally indicates the current branch. It can also point directly to a commit: this is detached HEAD mode. On a drawing, I begin by placing these labels. They explain why two people looking at the same history may be working in different places.

In this article's cover, main and atelier share commit A, then each receives a new commit. B and C therefore have the same parent. That situation is enough to pose a concrete question: what will Git do if we merge atelier into main?

The drawing becomes a testing ground

The shared link leads to Explain Git with D3, a project by onlywei. It provides command-specific simulation spaces: type a Git command and observe its effect on the graph. Exercises cover commits, branches, merge, rebase and reset, among others. A direct link to the merge exercise appears in the sources at the end.

For a beginner, this allows experimentation without touching their project. For a mentor, the graph provides a shared aid for explanation. I suggest pausing before every command: ask where the labels will be after execution, then compare that prediction with the displayed result.

The simulator uses git checkout to switch branches, among other commands. Its commands correspond to its interface; do not assume it reproduces every capability of a Git terminal.

What merge adds to the history

With main on B and atelier on C, switch to main before running git merge atelier. The merge brings together changes from both branches since their common ancestor A. If successful, it creates commit M here with two parents, B and C. main advances to M; atelier remains on C.

The drawing also helps explain fast-forward. If main had received no commit after A, Git could simply advance main to C. In that case, with default behaviour, no additional merge commit would be needed.

A conflict concerns the content to combine. When Git cannot decide how to combine changes, the merge stops so the conflict can be resolved. A graph explains links between commits; the files are still needed to choose the expected result.

Watch commit identifiers too

For comparison, we can return to the initial divergence and rebase atelier onto main. In this simple example, Git replays C's change on B and produces a new commit C′. Its parent changes, and so does its identifier. The atelier branch then points to C′. This explains why rebasing requires coordination when someone else is already working from the affected commits.

Before the rebase, B and C both have A as parent. After rebasing atelier onto main, main stays on B and atelier points to C′, whose parent is B. The old C does not become C′: a new commit is created.
Lines connect parents. The drawing isolates the simple rebase described; atelier no longer points to the old C, which does not mean it is immediately deleted. Reset's effects on files are a separate subject.

With git reset and a target commit, you move the current branch to that target. Options also determine what happens to the index, where the next commit is prepared, and to working files. --soft preserves both, --mixed resets the index to the target state and preserves the files, and --hard resets both to that state and can lose local work.

The site does not model staging files with git add and supports only --hard in its reset simulation. This simplification helps observe the graph but leaves out an essential part of real behaviour. A commit no longer appearing in a branch's history is not necessarily erased immediately: the local reflog can help find a previous position while the corresponding objects still exist.

Repeat the experiment in a disposable repository

With Git installed and a commit identity configured, here is an exercise to run in a new folder. Commits are deliberately empty: the shape of history is isolated, without claiming to test file merging or a conflict.

Terminal
mkdir git-visuel
cd git-visuel
git init -b main
git commit --allow-empty -m "A : point de départ"
git checkout -b atelier
git commit --allow-empty -m "C : travail sur atelier"
git checkout main
git commit --allow-empty -m "B : travail sur main"
git merge atelier -m "M : fusion de atelier"
git log --graph --oneline --decorate --all

Before the merge, draw main and atelier's positions. After the command, check that main points to the merge commit and atelier remains on C. To display M's two parent identifiers, use git show -s --format=%P HEAD.

During your next mentoring session, let the learner hold the pencil or type the commands. Ask them to predict the graph's next state and explain what they observe. You will know which point to revisit together instead of adding another command to memorise.

Sources and useful links

Share on LinkedIn (new tab)

Keep reading
All articles in this category