- Developers who already know basic commit and branch actions
- Readers who want to understand command boundaries and risk
Command Reference
git branch Tutorial
Covers how git branch lists, creates, renames, and deletes branches, and how it relates to remote-tracking branches.
- A basic mental model of worktree, index, and commits
- Comfort reading `git status` and a small commit graph
- Using local cleanup commands on already shared history
- Continuing to rewrite before confirming a recovery path
Data & Performance
- 41 bytesa branch is just a ref file containing the commit SHA — near-zero cost to createSource: Pro Git §3.1
Key Quotes
A branch in Git is simply a lightweight movable pointer to one of the commits — a 41-character SHA-1 ref stored under .git/refs/heads.
Citations & Further Reading
- Git branch [Official]
- Git Branching Branches in a Nutshell [Book]
- Git Branching Remote Branches [Book]
What you will learn
- Understand the core purpose of git branch Tutorial
- Master the basic usage and common options of git branch Tutorial
- Covers how git branch lists, creates, renames, and deletes branches, and how it relates to remote-tracking branches.
- Understand key concepts: Listing branches
- Know when to use this feature and when to avoid it
Start with a problem
You're working in a Git repository and need to perform a specific task — but you're not sure which command or option is the right fit, or what this command can and cannot do.
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 branchto list all local branches and quickly understand what development lines exist. - Use
git branch -vvto see each branch's upstream tracking relationship and latest commit — a frequent pattern for diagnosing sync status. - Use
git branch -dto safely delete already-merged branches and keep the branch list tidy; confirm you want a force delete with-D.
Diagram view
Special cases and boundaries
git branchcreates branch refs only; it does not switch to them — usegit switchfor 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 -drefuses to delete unmerged branches as a safety measure; use-Dto 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.
Try it yourself
- Practice the git-branch command in a test repository and observe state changes before and after
- Experiment with different options and compare the output differences
- Simulate a real scenario where you would need to use this, and walk through the full process
Further reading
Keep going on the same topic: