Branching Basics

Course 1 · Ch 4
Branching Basics
What a branch really is, creating and switching between them, and merging your work back together

Branches are the feature that makes git genuinely powerful rather than just "version control with extra steps." A branch lets you work on something — a new feature, an experiment, a fix — completely isolated from your main project, then bring it back in safely once it's ready. This chapter demystifies what a branch actually is under the hood, because the mental model matters more than the commands themselves.

What a Branch Actually Is

This surprises most beginners: a branch is just a movable label pointing at a commit. That's it. It's not a copy of your files, not a separate folder, not a snapshot of the whole project — just a lightweight named pointer. This is exactly why creating a branch in git is instant, even on huge projects, while in older version control systems branching could take minutes and gigabytes of disk space.

🏷️ A branch = a label
When you create a branch, git just writes a small file containing a commit hash. Switching branches moves that label, and updates your working directory to match whatever that commit looked like.
📍 HEAD = "where you are"
HEAD (from Chapter 3) is itself usually just a pointer to whichever branch label you currently have checked out. Switch branches, and HEAD moves to point at the new one.
➡️ The label moves automatically
Every time you commit while on a branch, that branch's label automatically moves forward to point at the new commit. You never move it manually — committing does it for you.
master feature/login c1 c2 c3 c4 c5
master stays at c3 while feature/login moves ahead through c4 and c5 — both branches share the same history up to c3.

Creating and Switching Branches

$ # Create a new branch (doesn't switch to it yet)
$ git branch feature/login

$ # List all branches — * marks the current one
$ git branch
feature/login
* master

$ # Switch to it
$ git switch feature/login
Switched to branch 'feature/login'

$ # Create AND switch in one step — the one you'll actually use most
$ git switch -c feature/login
git switch vs the older git checkout
You'll see git checkout feature/login in a lot of older tutorials and Stack Overflow answers — it does the same job. git switch is newer (Git 2.23+) and was specifically introduced because checkout did too many unrelated things (switching branches, restoring files, detaching HEAD) bundled into one confusing command. Both still work; switch is the clearer choice going forward.

Naming branches well

  • Use a consistent prefix: feature/login-form, fix/header-overflow, docs/readme-update — makes branch lists scannable at a glance, especially once a few accumulate.
  • Keep them short-lived where possible. A branch that lives for months drifts further from master every day, making the eventual merge harder — Chapter 9 covers what happens when this goes wrong across a team.
  • No spaces, use hyphens. Git branch names can't contain spaces; hyphens or underscores are the convention.

Doing Work on a Branch

Once switched, everything behaves exactly as in Chapters 2–3 — edit files, git add, git commit — except every commit you make extends this branch specifically, leaving master completely untouched in the meantime.

$ (feature/login) git add login.js
$ (feature/login) git commit -m "Add password strength validation"
$ (feature/login) git switch master
Switched to branch 'master'
# login.js reverts to how it looked on master — the feature/login changes are still safe, just not here

Merging — Bringing Branches Back Together

Once work on a branch is ready, git merge combines it back into another branch — typically, you switch to the destination branch first, then merge the feature branch into it.

$ (master) git switch master
$ (master) git merge feature/login
Updating f9e8d7c..a1b2c3d
Fast-forward
login.js | 4 ++++
1 file changed, 4 insertions(+)
Merge typeWhen it happensWhat you'll see
Fast-forward master hasn't moved at all since the branch was created — git just moves master's label forward "Fast-forward" in the output, no new merge commit created
Three-way merge master gained other commits while your branch was being worked on — git combines both histories A new "merge commit" is created, with two parent commits
Merge conflict both branches changed the same lines of the same file — git can't decide automatically Git pauses and asks you to resolve it manually (full coverage in Course 2, Chapter 3)
Fast-forward merges are the easy case — don't expect every merge to look like this
The fast-forward example above only happens because nothing else changed on master while feature/login was being developed. The moment two branches have genuinely diverged, git needs an actual three-way merge, and if the same lines were touched by both, a conflict. This is completely normal and not a sign anything went wrong — Course 2 dedicates a full chapter to resolving conflicts confidently.

Cleaning up after a merge

$ # Once merged, the branch has done its job — delete it
$ git branch -d feature/login
Deleted branch feature/login (was a1b2c3d).

Deleting a merged branch only removes the label — every commit it pointed to is still in history, reachable from master. Nothing is lost; you're just tidying up branch names you no longer need.

Command Reference

CommandWhat it does
git branchList branches; current one marked with *
git branch <name>Create a new branch (doesn't switch to it)
git switch <name>Switch to an existing branch
git switch -c <name>Create and switch to a new branch in one step
git merge <name>Merge the named branch into your current branch
git branch -d <name>Delete a branch (only its label — commits remain in history)

Chapter 4 Quick Reference

  • A branch is just a movable label pointing at a commit — not a copy of your files
  • HEAD points to whichever branch you currently have checked out
  • git switch -c <name> — create and switch in one step (the one you'll use most)
  • git merge <name> — run from the destination branch, merging the named branch in
  • Fast-forward = simple case, no diverging history; three-way merge = both branches moved; conflict = same lines changed in both
  • git branch -d — safe to delete a merged branch; its commits remain in history
  • Next chapter: connecting to GitHub — remotes, push/pull/clone, and your first real backup off your own machine