Branches

A branch is a movable pointer to a commit, letting you develop features in isolation.

Syntaxgit branch <name>

A branch is a lightweight, movable pointer to a commit. The default branch is usually main. Creating a branch lets you work on something new without touching stable code.

Working with branches

  • git branch — list branches; the current one is marked with *.
  • git branch feature-x — create a new branch.
  • git branch -d feature-x — delete a merged branch.

Branches are cheap and fast in Git because they are just a 40-character pointer, not a copy of your files.

Example

Example · bash
# List branches
git branch

# Create a new branch called login-page
git branch login-page

# Nothing is switched yet — you are still on main

When to use it

  • A developer creates a feature branch so new code is isolated from main until it passes code review.
  • A release manager maintains a release/1.4 branch to apply hotfixes without pulling in unreleased features from main.
  • A solo developer keeps a branch per GitHub issue so they can context-switch between tasks without mixing changes.

More examples

Create and list branches

Creates a lightweight pointer to the current commit and lists all local branches, with an asterisk on the active one.

Example · bash
# Create a new branch
git branch feature/user-auth

# List all local branches
git branch
# * main
#   feature/user-auth

Create branch from a specific commit

Shows that a branch can start from any commit in history, not just the current HEAD.

Example · bash
# Branch off an older commit for a hotfix
git branch hotfix/login-crash 9b0d14a

# List with last-commit info
git branch -v

List all local and remote branches

The -a flag shows both local branches and every tracked remote-tracking branch in a single view.

Example · bash
git branch -a
# * main
#   feature/user-auth
#   remotes/origin/main
#   remotes/origin/feature/payments

Discussion

  • Be the first to comment on this lesson.
Branches — GitHub | SoundsCode