CH 21 / 75Part 4: Branches and Merging

What Is a Branch?

What you will learn on this page

  • Explain a branch as "a sticky note attached to a commit"
  • Understand that when you commit, only the branch HEAD is attached to moves forward
  • Check where branches are with git branch and git log --graph --all
  • Understand what you gain by splitting work into branches
On this page7 items

In Part 3, #19, we found that a branch is really just a 41-byte file containing a single commit ID. This begins Part 4. Building on that, we’ll take a fresh look at what branches are for and how they move when you commit.

A branch is “a sticky note attached to a commit”#

“Branch” suggests a limb of a tree. The name tends to make you imagine “a separate workspace with a full copy of the history,” but that’s not what a Git branch is. A branch is a named sticky note attached to a single commit.

No matter how many sticky notes you attach, not a single commit or file is added. The only thing that differs is “which sticky note is attached to which commit.”

In the practice repository, check which branches exist right now.

text2 lines
cd ~/git-practice
git branch
text1 lines
* main

git branch is the command that lists branches. Right now there’s only main, and the leading * marks “the branch you’re on,” that is, the branch HEAD is attached to (Part 3, #18).

Now let’s add one more sticky note.

text2 lines
git branch idea
git branch
text2 lines
  idea
* main

git branch <name> creates a new branch at the commit you’re on. Creating it doesn’t move you, so the * stays on main.

text1 lines
git log --oneline --decorate -n 1
text1 lines
370768c (HEAD -> main, tag: v1.0, idea) Add cake recipe

--decorate is an option that lists the names pointing at a commit inside ( ). You can see that the commit 370768c has three names: main, idea and the tag v1.0. HEAD -> main means “HEAD is attached to main.”

On the left, right after git branch idea, two sticky notes, main (with HEAD attached) and idea, point to the same commit C. On the right, after one commit on main, main and HEAD have moved to the new commit D while idea stays on C
A branch is a named sticky note attached to a commit

When you commit, only the branch HEAD is attached to moves#

If you commit while there are two sticky notes, which one moves?

text3 lines
echo "Clean the room" >> todo.txt
git commit -am "Add cleaning to the to-do list"
git log --oneline --decorate --graph --all -n 3
text3 lines
* 4e16e23 (HEAD -> main) Add cleaning to the to-do list
* 370768c (tag: v1.0, idea) Add cake recipe
* 2d6c77c Revert "Add post office to the to-do list"

--graph is an option that draws how the history connects using symbols on the left edge, and --all shows the history of every branch, not just the one you’re on. In Part 4, which deals with branches, you’ll use this combination again and again.

Only main moved to the new commit 4e16e23; idea stayed on the previous commit. As we saw in Part 3, #18, when you commit, only the sticky note of the branch HEAD is attached to is moved to the new commit. Other branches don’t move.

git branch -v lists which commit each branch points to.

text1 lines
git branch -v
text2 lines
  idea 370768c Add cake recipe
* main 4e16e23 Add cleaning to the to-do list

Diverging: how a branch becomes “a separate workspace”#

Since you’re not on idea right now, idea stays put. What happens if you switch to idea and commit there? idea moves from C (370768c) to a new commit, while main is on D (4e16e23), so you get two lines of history that share the same parent, C. This is “diverging.”

text3 lines
          ┌─ D  ← main
A ─ B ─ C ┤
          └─ E  ← idea

A branch is just a sticky note, yet it can be used as “a separate workspace” because two things work together:

  1. When you commit, only the branch you’re on moves (other branches aren’t affected)
  2. When you switch branches, the files in the working tree are swapped to match that branch’s commit (covered next, in #22)

Thanks to these two, you can experiment on idea without breaking main, bring it into main if it works out, and throw idea away if it doesn’t. Bringing it in is the merge, covered in #23.

What you gain by splitting work into branches#

SituationWithout branchesWith separate branches
You want to try a major rewriteHard to undo if it failsThrow away the whole branch and you’re back where you started
An urgent fix comes in mid-taskIt gets mixed with your half-written changesGo back to main to fix it, and resume your work later
You want to work on several things in parallelEverything gets jumbled in one historyEach task gets its own history, and you can bring them in one at a time later

Branches take an instant to create or delete and use almost no space (Part 3, #19). Feel free to create a branch even for “just trying something.”

We won’t use the practice branch idea this time, so delete it.

text1 lines
git branch -d idea
text1 lines
Deleted branch idea (was 370768c).

Only the sticky note is gone; the commit 370768c remains as part of main’s history.

FAQ#

Q. Is main a special branch?
A. To Git, it’s just a sticky note like any other branch. The only differences are that it’s created automatically at the start and that, by convention, it’s used as the place for “the finished, official history.” Its name is the one you set with init.defaultBranch in Part 2, #06.

Q. When you create a branch, are files copied into a new folder?
A. No. There’s still only one folder, and switching branches swaps its contents. If you want to open several branches in separate folders at the same time, use worktrees, covered in Part 5, #33.

Summary#

  • A branch is a named sticky note attached to one commit. Creating one adds no files or commits
  • git branch lists branches, and git branch <name> creates a new branch at the commit you’re on
  • When you commit, only the branch HEAD is attached to moves to the new commit
  • When two branches each move forward from the same commit, the history diverges
  • git log --oneline --decorate --graph --all shows where every branch is and how they connect

Next page#

22. Creating, Switching and Deleting Branches ── Move between branches with git switch and watch the contents of the working tree change.