Skip to main content

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:
branch early, and branch often
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:
newImage (branch) ---> C1 (commit)
The branch itself contains no data - it’s just a movable pointer.

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 a main branch (or master in older Git versions) that points to the initial commit.

Creating Branches

1

Create a new branch

Use git branch followed by the branch name:
git branch bugFix
This creates a new branch pointing to the current commit.
2

Switch to the branch

Use git checkout to move to the new branch:
git checkout bugFix
Now the asterisk (*) appears on bugFix.
3

Shortcut: Create and switch

Combine both steps with -b flag:
git checkout -b 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:
main*
  |
  C1
After git commit:
main*
  |
  C2
  |
  C1
The main branch now points to C2.

Non-Active Branches Stay Put

Branches you’re not on don’t move: Starting state:
main*        newImage
  |            |
  C1 -------- C1
After git commit on main:
main*
  |
  C2          newImage
  |            |
  C1 -------- C1
Only main moved; newImage stayed at C1.

The Branching in Git Level

The introductory level teaches branching fundamentals:

Level Goal

1

Create bugFix branch

git branch bugFix
2

Switch to bugFix

git checkout bugFix
Or use the shortcut:
git checkout -b bugFix

Expected Result

main        bugFix*
  |            |
  C1 -------- C1
  |
  C0
Both branches point to C1, but bugFix is now active.

Branch Divergence

Branches enable parallel development:
1

Create and switch to bugFix

git checkout -b bugFix
2

Commit on bugFix

git commit
Creates C2 on the bugFix branch.
3

Switch back to main

git checkout main
4

Commit on main

git commit
Creates C3 on the main branch.

Result: Divergent History

       C3 (main*)
      /
C0-C1
      \
       C2 (bugFix)
Two separate lines of development from a common ancestor.

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:
git checkout -b new-feature
# Make changes and commit
git commit
git commit
Keeps the main branch stable while developing.

Bug Fix Branches

Isolate bug fixes:
git checkout -b bugfix-123
# Fix the bug
git commit
Easy to test and merge when complete.

Experimental Branches

Try ideas without risk:
git checkout -b experiment
# Try something new
git commit
Delete if it doesn’t work out, or merge if it succeeds.

Branch Implementation Details

From the source code, branches have these properties:
{
  id: "main",                    // Branch name
  target: "C3",                   // Commit it points to
  remoteTrackingBranchID: "o/main" // For remote tracking (if any)
}

HEAD and Branches

The special HEAD reference points to the current branch:
HEAD --> main --> C3
When you commit, the branch HEAD points to moves forward.

Detached HEAD

HEAD can also point directly to a commit:
HEAD --> C2
This is “detached HEAD” mode - commits made here won’t be on any branch.

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

git branch
Shows all branches, with * marking the current one.

Delete a Branch

git branch -d branchName
Removes the branch pointer (commits remain if referenced elsewhere).

Rename a Branch

git branch -m oldName newName

Advanced Branching

Remote Tracking Branches

Branches can track remote counterparts:
main --> C3
o/main --> C3  // Remote tracking branch
More on this in Remote Repositories.

Branch with Specific Commits

git branch newBranch C2
Creates a branch pointing to commit C2.

Next Steps

Now that you understand branching:
  1. Learn how to combine branches using merge
  2. Explore rebasing for linear history
  3. Practice with multiple branches to manage complex workflows

Build docs developers (and LLMs) love