Tour of Git
Branching & Merging / Lesson 2.1

What are Branches?

A branch is an independent line of development. It lets you work on a feature, bug fix, or experiment without affecting the main codebase.

The Big Picture

Think of branches like parallel timelines:

main:     A --- B --- C
                 \
feature:          D --- E
  • main continues forward with commit C
  • feature branched off from B and has its own commits D and E
  • They don't interfere with each other

Why Use Branches?

Isolation — Work on a feature without breaking main. If the experiment fails, just delete the branch.

Collaboration — Multiple developers work on different branches simultaneously, then merge when ready.

Code Review — Create a branch, push it, and open a pull request for others to review before merging.

How Branches Work Internally

A branch is just a pointer to a commit. That's it. Creating a branch doesn't copy any files — it just creates a new pointer.

main ──→ commit C
feature ──→ commit E
HEAD ──→ feature  (you're currently on "feature")
  • HEAD is a special pointer that tracks which branch you're currently on
  • When you commit, the current branch pointer moves forward
  • When you switch branches, HEAD moves to the other branch

The Default Branch

When you run git init, Git creates a default branch called main (or master in older versions). This is just a convention — there's nothing technically special about it.

Common Branch Workflow

  1. Create a branch: git branch feature
  2. Switch to it: git checkout feature
  3. Make commits on the branch
  4. Switch back: git checkout main
  5. Merge the work: git merge feature
  6. Delete the branch: git branch -d feature

In the next lessons, you'll practice each of these steps.