Gitastic
Path › Part 2: Branching › Lesson 4 of 16
04

Branches are just pointers

A branch is a sticky note on a commit. That is all it is.

1 Learn the idea

People imagine a branch as a copy of the project, or as a separate folder. It is neither. A branch is a name that points at one commit — and nothing more.

HEAD: "you are here"

HEAD is a special pointer that says which branch you are currently on. When you commit, git creates the new commit with the current commit as its parent, then moves the branch that HEAD points to. Only that one branch moves.

  • Creating a branch is instant — git just writes a 41-byte file containing a hash. No files are copied.
  • Switching branches moves HEAD and swaps the files in your folder to match that commit's snapshot.
  • Deleting a branch removes the sticky note, not the commits (though commits nobody points to are eventually cleaned up).

2 Watch it happen

Sticky notes in action

Press Play or use → to step through. Watch the graph as each command runs.

56194a0 Start recipe book56194a0Start recipe b…8830919 Add pancakes8830919Add pancakesHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Working directoryyour folder / editor
README.md
pancakes.md
git add →← git restore
Staging areathe next snapshot (index)
README.md
pancakes.md
git commit →← restore --staged
RepositoryHEAD → 8830919
README.md
pancakes.md
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git branch

    Two commits, one branch. main points at the latest commit, and HEAD points at main (the * in the list).

  2. $ git branch soup

    git branch soup sticks a new note onto the same commit. No files copied, no new commits — the graph barely changes.

  3. $ git switch soup

    git switch moves HEAD to soup. Both branches still point at the same commit, so your files do not change.

  4. $ echo "Tomato soup: tomatoes, onion, basil" > soup.md
    $ git add soup.md
    $ git commit -m "Add tomato soup"

    We commit on soup. Watch: only the soup note moved forward. main stayed behind, because HEAD was not pointing at it.

  5. $ git switch main

    Back on main. HEAD jumps back, and soup.md disappears from the working directory — main's snapshot doesn't have it. It is safe inside the soup commit.

  6. $ echo "Waffles: flour, eggs, butter" > waffles.md
    $ git add waffles.md
    $ git commit -m "Add waffles"

    A commit on main. History now forks: two branches grew in different directions from a common ancestor.

  7. $ git switch -c dessert

    git switch -c = create a branch and switch to it in one go. The most common way to start new work.

  8. $ git switch main
    $ git branch -d dessert

    dessert had no unique commits, so deleting it is harmless. Deleting a branch removes the note, never the shared commits.

  9. $ git branch -d soup

    Git refuses to delete soup: its commit is not part of main, so the note is the only thing keeping it findable. -D would force it.

Myth
  • A branch is a copy of my files
  • Branches are expensive, make few
  • Deleting a branch deletes its work immediately
Reality
  • A branch is a pointer to one commit
  • Branches cost ~nothing — make one per task
  • It deletes the pointer; git warns if commits would be orphaned
git branchList branches (* marks where HEAD is)
git switch -c <name>Create a branch here and move onto it
git switch <name>Move HEAD to another branch
git branch -d <name>Delete a (merged) branch pointer

3 Check your understanding

Question 1

What is a branch in git, technically?

Question 2

You are on feature (see graph). You make a new commit. Which labels move?

b01499b Initialb01499bInitial032d09a Setup tooling032d09aSetup toolingHEAD = where you are. It points at the current branch.HEAD →Branch "feature" — a movable pointer (current branch: moves when you commit)featureBranch "main" — a movable pointermain
Question 3

Why does a file sometimes "disappear" when you git switch to another branch?

Question 4

git branch -d experiment fails with "not fully merged". What does that tell you?

4 Practice for real

Your mission

Work on a side branch

Create a branch called feature, make one commit on it, and then return to main. main must not contain the new commit.

220addd Initial commit click to insert the hash into the terminal220adddInitial commitHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Working directoryyour folder / editor
README.md
git add →← git restore
Staging areathe next snapshot (index)
README.md
git commit →← restore --staged
RepositoryHEAD → 220addd
README.md
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (main) $