The Tree of Possibilities
Branches, switch and parallel timelines
Introduction
The Master Archivist summons you to a room you had never seen before. At its center, an immense crystal tree rises from floor to ceiling. Its translucent branches pulse with a soft glow, and each one seems to contain moving images - fragments of different stories, parallel futures of the kingdom.
"Apprentice, until now you have worked on a single timeline - a unique, linear chronicle where each event follows the previous one. But great Archivists know something novices do not: history does not have just one possible path. Look at this tree. Each branch represents a parallel timeline - a possible future for the kingdom. You can explore these branches, record events on them, and decide later which one will become the official chronicle. This is the power of Git branches."
What is a branch?
Until now, all your commits have been chained on a single branch: main (formerly "master"). It is the official chronicle - the main timeline of the project.
Why use branches?
- Experiment without risk: you test an idea on a branch. If it works, you keep it. If not, you delete it.
- Work in parallel: while a colleague works on one feature, you work on another, each on your own branch.
- Keep
mainclean: the main branch only contains validated and tested code.
View existing branches
To list all branches in your repository:
git branch The current branch is marked with an asterisk * and appears in green:
* main For now, you only have one branch. That is about to change.
Create a new branch
To create a branch without switching to it:
git branch <name> Example:
git branch expedition-nord This creates a branch expedition-nord that points to the same commit as your current branch. But you are still on main - you have not changed timelines.
Verify:
git branch expedition-nord
* main The asterisk is still on main.
Switch to a branch
The modern command: git switch
git switch expedition-nord This is the recommended method since Git 2.23. Simple, clear, unambiguous.
The old method: git checkout
git checkout expedition-nord You will see git checkout in many tutorials and existing projects. It works, but git switch is more explicit because git checkout also does other things (like restoring files, for example).
Use git switch to change branches. It is clearer and helps avoid mistakes.
Create and switch in one command
You can combine creation and switching:
git switch -c new-branch Or with the old syntax:
git checkout -b new-branch HEAD - The position marker
HEAD is a special pointer that indicates where you currently are in the history. More precisely, HEAD points to the current branch, which itself points to a commit.
HEAD -> main -> commit C3
expedition-nord -> commit C3 When you run git switch expedition-nord:
main -> commit C3
HEAD -> expedition-nord -> commit C3 And when you make a new commit on expedition-nord:
main -> commit C3
HEAD -> expedition-nord -> commit C4 Delete a branch
Safe deletion
git branch -d branch-name The -d option (lowercase) deletes the branch only if it has been merged. This is a safety measure: Git refuses to delete a branch that contains unintegrated work.
Forced deletion
git branch -D branch-name The -D option (uppercase) deletes the branch even if it has not been merged. Use with caution - unmerged work will be lost (well, not truly lost, but much harder to recover).
You cannot delete the branch you are currently on. Switch to another branch first.
Practical exercise - The Parallel Chronicles
The kingdom faces a dilemma. Two expeditions must be documented: one heading North, toward the Frost Mountains, and one heading South, into the Emerald Forest. Each expedition deserves its own chronicle, its own timeline. It is up to you to create them, Archivist.
Step 1 - Create the repository
Create a new folder and initialize a Git repository:
mkdir chroniques-paralleles
cd chroniques-paralleles
git init -b main Step 2 - First commit on main
Create a base file and make your first commit:
echo "# Chronicles of the Kingdom" > chroniques.md
git add chroniques.md
git commit -m "Create the kingdom chronicles registry" Step 3 - Create the expedition-nord branch
git branch expedition-nord Step 4 - Switch to expedition-nord
git switch expedition-nord Verify that you are on the right branch:
git branch You should see the asterisk on expedition-nord.
Step 5 - Commit on expedition-nord
Create a file and commit it:
echo "Day 1: The expedition leaves the city towards the Frost Mountains." > nord.md
git add nord.md
git commit -m "Add the northern expedition journal" Step 6 - Return to main
git switch main Notice: the file nord.md has disappeared from your folder! It only exists on the expedition-nord branch. Don't panic, it is safe in that timeline.
Step 7 - Create and switch to expedition-sud
This time, combine both in a single command:
git switch -c expedition-sud Step 8 - Commit on expedition-sud
echo "Day 1: The expedition ventures into the Emerald Forest." > sud.md
git add sud.md
git commit -m "Add the southern expedition journal" Step 9 - List all branches
git branch You should see three branches: expedition-nord, expedition-sud (with the asterisk), and main.
Step 10 - Verify
Run the verification script to validate your quest:
Bash (Linux / macOS / Git Bash on Windows):
bash verifier.sh PowerShell (Windows):
.\verifier.ps1 The script checks:
- You are in a Git repository
- The
expedition-nordbranch exists - The
expedition-sudbranch exists - The
expedition-nordbranch contains at least one unique commit - The
expedition-sudbranch contains at least one unique commit - There are at least 3 branches in total
- You are back on the
mainbranch
Command summary
| Command | Description |
|---|---|
git branch | List branches |
git branch <name> | Create a branch |
git switch <name> | Switch to a branch |
git checkout <name> | Switch to a branch (old syntax) |
git switch -c <name> | Create a branch and switch to it |
git checkout -b <name> | Create and switch (old syntax) |
git branch -d <name> | Delete a branch (safe) |
git branch -D <name> | Delete a branch (forced) |
You contemplate the Tree of Possibilities. Three branches now glow with a distinct light - three parallel timelines, each telling a different story. The Master Archivist places his hand on your shoulder.