Command Reference

git branch Tutorial

Covers how git branch lists, creates, renames, and deletes branches, and how it relates to remote-tracking branches.

Who This Is For
  • Developers who already know basic commit and branch actions
  • Readers who want to understand command boundaries and risk
Prerequisites
  • A basic mental model of worktree, index, and commits
  • Comfort reading `git status` and a small commit graph
Common Risks
  • Using local cleanup commands on already shared history
  • Continuing to rewrite before confirming a recovery path

The short version

git branch manages branch refs, which are names that point at commits.

Listing branches

git branch
git branch -r
git branch -a
git branch -vv

-vv is especially useful because it shows upstream tracking information and the latest commit summary.

Creating a branch

git branch feature/login

This creates the branch but does not switch to it. For that, pair it with git switch -c or use the older checkout form.

Renaming and deleting

git branch -m old-name new-name
git branch -d feature/login

Use -D only when you deliberately want a force delete.

Local vs remote-tracking refs

People often confuse main with origin/main. The first is your local branch. The second is a remote-tracking ref updated by fetch. Understanding that distinction helps explain many sync workflows.

What problem this command solves in a workflow

git branch operates at the reference layer, responsible for creating, renaming, listing, and deleting branches. It does not touch working tree content or the staging area — its core duty is managing "names that point at commits," letting you organize work across different development lines.

Typical use cases

  • Use git branch to list all local branches and quickly understand what development lines exist.
  • Use git branch -vv to see each branch's upstream tracking relationship and latest commit — a frequent pattern for diagnosing sync status.
  • Use git branch -d to safely delete already-merged branches and keep the branch list tidy; confirm you want a force delete with -D.

Diagram view

Creation and management of branch referencesBranches are essentially references pointing at commits. The branch command manages CRUD operations on these refs, without touching file content.
Input
Branch nameTarget commit (when creating)Current HEAD
Output
New ref under refs/heads/Updated branch listDeleted refs
branch only manages the refs themselves; use switch/checkout to switch branches and merge/rebase to integrate changes.

Special cases and boundaries

  • git branch creates branch refs only; it does not switch to them — use git switch for that.
  • Deleting a branch does not delete commits: as long as other refs or the reflog still point at them, the commit objects remain.
  • git branch -d refuses to delete unmerged branches as a safety measure; use -D to force when you are sure.
  • Remote-tracking refs (e.g., origin/main) and local branches (e.g., main) are independent: fetch updates the former, and you need merge or rebase to integrate into the latter.