LightMySky
En anglais

Version Control as a Graph of Commits

In Git, history is a directed graph of snapshot commits. A root commit has no parents, an ordinary commit has one, and a merge can have several. Branches are movable labels. HEAD points to the current branch name or directly to a commit.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Draw the commit graph of a short history and mark where each branch label sits
  • Say what changes in the graph when a commit is made and when a branch is created
  • Explain detached HEAD as a direct pointer to a commit rather than a pointer to a branch name

1 · Read

Git can save a program's stages as commits. Each commit records a snapshot and links to its parent commits. A root commit has no parents. An ordinary commit has one; a merge can have two or more. To draw the history, draw one node per commit and an arrow from each commit to each parent. Parent links point to commits that already existed, so following them cannot lead back to the starting node.

J: no parentsK: parent JL: parent K
These records name the parents. To draw their graph, add arrows from K to J and from L to K.

A branch is a movable label pointing to a commit. HEAD usually points to the current branch name. When you make an ordinary commit on that branch, the new commit's parent is the branch's previous tip. The branch moves to the new commit, while HEAD still points to the branch name. Other branches stay put.

Try it together

Start with root commit J, then make ordinary commits K and L on main. HEAD points to main. Creating draft at L adds a label but no commit and does not switch HEAD. Switch to draft, then make M. M has parent L, draft moves to M, main stays at L, and HEAD still points to draft. To reconstruct the graph, draw arrows from M to L, L to K and K to J, with none from J. Place main at L and draft at M.

main: Ldraft: MHEAD: draftM: parent L

Each record names a pointer target or a parent. The branch pointers differ, but M still links back to L.

Checking out a commit by its ID makes HEAD point directly to that commit, rather than to a branch name. This is detached HEAD, even if a branch points to the same commit. An ordinary commit made here uses HEAD's previous commit as its parent. HEAD moves to the new commit, but no branch moves.

main: LHEAD directly: L

After checking out L by its ID, main can stay at L while HEAD points directly to L.

After making detached commits, switching to main makes HEAD point to main. It does not merge or immediately delete the detached commits. If no branch leads to them, they can be harder to find. A local reflog can record recent moves of HEAD and branch references and may help you find them for a time. Its entries can expire.

Before switching away from detached work, create a branch at its latest commit. A commit is reachable from a branch if it is the branch's tip or you can follow parent links from the tip to it. If detached commits T then U were added after L, a branch at U keeps U, T and L reachable. A branch at L does not reach T or U because parent links lead toward older commits.

saved: UU: parent TT: parent L

Start at saved's tip, U. Its parent links reach T and L. Starting at L cannot take you forward to T or U.

Commits link to parents. Branches point to commits. HEAD points to a branch name or directly to a commit. A branch at detached work's latest commit keeps that history reachable.

Git commit-history lesson revisions, questions and flashcard revisions written for this dataset.

2 · Watch

En anglais

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Version Control as a Graph of Commits · Computing, ages 18 to 19 · LightMySky