What is a Branch?
Branches in Git are incredibly lightweight - they are simply pointers to a specific commit, nothing more. This simplicity is why many Git enthusiasts follow the mantra:Because there’s no storage or memory overhead with making many branches, it’s easier to logically divide your work than to have big, unwieldy branches.
How Branches Work
A branch essentially says: “I want to include the work of this commit and all its parent commits.”Branch as a Pointer
When you create a branch, you’re creating a reference that points to a commit:Branches in the Visualization
Visual Representation
Branches appear as labels attached to commits:- The branch name (e.g.,
main,bugFix) - An asterisk (*) marks the currently checked-out branch
- Branch colors help identify which commits belong to which branches
The Default Branch
Every repository starts with amain branch (or master in older Git versions) that points to the initial commit.
Creating Branches
Create a new branch
Use This creates a new branch pointing to the current commit.
git branch followed by the branch name:Switch to the branch
Use Now the asterisk (*) appears on
git checkout to move to the new branch:bugFix.Git 2.23 introduced
git switch as a clearer alternative to git checkout for branch operations. Both commands work in Learn Git Branching.Branch Behavior
Branches Move with Commits
When you commit on a branch, that branch pointer moves forward: Before commit:git commit:
main branch now points to C2.
Non-Active Branches Stay Put
Branches you’re not on don’t move: Starting state:git commit on main:
main moved; newImage stayed at C1.
The Branching in Git Level
The introductory level teaches branching fundamentals:Level Goal
Or use the shortcut:Expected Result
C1, but bugFix is now active.
Branch Divergence
Branches enable parallel development:Result: Divergent History
Branch Properties
Lightweight Nature
No Storage Cost
Branches don’t duplicate files or commits - they’re just pointers.
Instant Creation
Creating a branch happens immediately, regardless of repository size.
Fast Switching
Switching branches is instantaneous because Git only updates pointers.
Unlimited Branches
Create as many branches as needed without performance impact.
Branch Colors
Learn Git Branching uses color coordination to help with learning:- Each branch has a unique color
- Commits are colored based on which branches contain them
- When branches merge, commit colors blend together
Color mixing shows branch relationships: if
main is blue and bugFix is orange, a commit containing both appears purple.Common Branching Patterns
Feature Branches
Create a branch for each new feature:Bug Fix Branches
Isolate bug fixes:Experimental Branches
Try ideas without risk:Branch Implementation Details
From the source code, branches have these properties:HEAD and Branches
The specialHEAD reference points to the current branch:
Detached HEAD
HEAD can also point directly to a commit:Best Practices
Descriptive Names
Use clear names like
feature-login or bugfix-crash rather than branch1.Branch Often
Create branches for each feature or fix to keep work organized.
Keep Branches Focused
Each branch should have a single, clear purpose.
Delete When Done
Remove merged branches to keep the repository clean.
Common Commands
List Branches
Delete a Branch
Rename a Branch
Advanced Branching
Remote Tracking Branches
Branches can track remote counterparts:Branch with Specific Commits
C2.
Related Concepts
- Commits - What branches point to
- Merging - Combining branches
- Rebasing - Moving branches to new bases
Next Steps
Now that you understand branching:- Learn how to combine branches using merge
- Explore rebasing for linear history
- Practice with multiple branches to manage complex workflows