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 |
|
|
|
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
Trunk-based by default
Short-lived feature branches (hours to days), merged to main daily. Long-lived branches diverge forever.
One branch per concern
fix/payment-retry, notmy-work. A branch is a name for a change, not a name for a developer.Delete after merge
A merged branch is noise. Delete it; the commits survive in the target.
Release branches for hotfixes
release/v1.2lets you patch production without merging unready features from main.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
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, notjohn-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
masterhas uncomfortable connotations in some contexts andmainis shorter and clearer. Purely a convention.
Connections
Chacon, S. & Straub, B. (2014). Pro Git, Chapter 3. https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
Git project (2024). git-branch(1). https://git-scm.com/docs/git-branch
Atlassian (2024). Git branch. https://www.atlassian.com/git/tutorials/using-branches
Etymology
Origin: metaphor
branch(noun): from Late Latin branca, "paw"; extended to the limb of a tree
Semantic network
● broader ● narrower ● related
Used in the garden
- Explore the garden (7×)
- Mastering Database Schema Version Control: The DevOps Secret Weapon (3×)
- Changelog (2×)
- About this site (1×)
- Master Linux Automation: Bash Scripting Secrets for Solo IT Founders (1×)
- The Office Plant That Became My Business Mentor (1×)
- ENDURE · Social (1×)
Sources
Chacon, S. & Straub, B. (2014). Pro Git. https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
Git project (2024). git-branch(1). https://git-scm.com/docs/git-branch
Atlassian (2024). Git branch. https://www.atlassian.com/git/tutorials/using-branches