Skip to content

Branch

A branch is a movable pointer to a commit in a version control system; creating a branch is O(1) because it only writes a 40-byte file, and switching branches rewrites the working tree to match the target commit.

Definition

What it is

In Git, a branch is a file at .git/refs/heads/main containing a 40-character SHA (or 64 with SHA-256) pointing to the latest commit on that branch. Creating a branch writes a new file with the same SHA. Switching branches (checkout) rewrites the working tree to match the target commit. Merging two branches finds their common ancestor and combines the changes.

This is radically different from older systems. In Subversion, a branch is a directory copy — cheap but not free. In Mercurial, a branch is a named line of commits. In Git, a branch is just a label: the "branching" is the fact that different commits have different parents.

Why it matters

Without cheap branches, every change must be coordinated with everyone else, and every experiment risks breaking the main code. With cheap branches, experimentation is free: create a branch, try the idea, delete the branch if it fails. Trunk-based development relies on this: developers branch off, work for hours, merge back, delete.

Branch vs tag vs commit

Aspect

Branch

Tag

Commit

Moves with commits

Yes

No (usually)

No

Points to

Latest commit

Specific commit

Its parent

Mutable

Yes

Usually not

No

Typical use

Parallel work

Release marker

Snapshot

Under the hood

.git/refs/heads/*

.git/refs/tags/*

.git/objects/*

Where branches live

graph TD
  C1[c1] --> C2[c2]
  C2 --> C3[c3]
  C3 --> C4[c4]
  C2 --> B1[b1 feature]
  B1 --> B2[b2 feature]
  C3 -.->|main| C4
  B2 -.->|feature| B2
  style C4 fill:#E8F5E9
  style B2 fill:#FFF3E0

Branch strategies

Branching without fear

  1. Trunk-based by default

    Short-lived feature branches (hours to days), merged to main daily. Long-lived branches diverge forever.

  2. One branch per concern

    fix/payment-retry, not my-work. A branch is a name for a change, not a name for a developer.

  3. Delete after merge

    A merged branch is noise. Delete it; the commits survive in the target.

  4. Release branches for hotfixes

    release/v1.2 lets you patch production without merging unready features from main.

  5. Feature flags before branches

    If the change is huge, use a feature flag instead of a long-lived branch. Flags merge continuously.

Common failures

Origin

The word comes from the botanical sense: branca in Late Latin, "paw" — extended to the limb of a tree. Version control adopted it as a metaphor: the "trunk" is the main line; "branches" diverge from it. CVS and Subversion used "branch" for directory copies, which were expensive. Git in 2005 redefined the concept: a branch is a pointer to a commit, costing one file. The refactor made branching so cheap that the entire workflow of software development shifted.

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master or main.

— Pro Git, Chapter 3, 2014
1972 · SCCS introduce "branch" en VCS
1990 · CVS usa branches como directorios
2005 · Git redefine branches como punteros
2020 · GitHub cambia default a `main`
40 · bytes que ocupa una branch ref

Evidence

FAQ

Why are Git branches cheap?

Because they are 40-byte files, not directory copies. Creating a branch does not copy files; it writes one reference.

How to name a branch?

By concern, not by author: fix/login-timeout, not john-dev. The name should predict the merge commit.

When to merge?

When the branch is ready and main has moved forward; daily in trunk-based development.

Why does GitHub default to `main`?

Because master has uncomfortable connotations in some contexts and main is shorter and clearer. Purely a convention.

Connections

Etymology

Origin: metaphor

  • branch (noun): from Late Latin branca, "paw"; extended to the limb of a tree

Semantic network

BranchGitversion-coCommitMergeRebaseStashGittrunk-base

● broader ● narrower ● related

Broader

  • Git
  • version-control

Related

Used in the garden

Sources