Listing Branches
Once you have more than two or three branches, the question “what branches exist?” comes up daily. git branch is the answer, with several useful flags for filtering and formatting. Knowing them turns branch management from a guessing game into something orderly.
List local branches
git branch # develop # * main ← * marks the current branch # feature-x # bugfix-42
List remote-tracking branches
git branch -r # origin/HEAD -> origin/main # origin/main # origin/develop # origin/feature-x
List ALL branches (local + remote)
git branch -a # develop # * main # feature-x # remotes/origin/HEAD -> origin/main # remotes/origin/main # remotes/origin/develop
Verbose mode — last commit on each branch
git branch -v # develop c204c1d Bump deps # * main 1f9ab2c Add login form # feature-x d4b1e0c WIP search # Even more info — also show tracking status git branch -vv # * main 1f9ab2c [origin/main] Add login form # feature-x d4b1e0c [origin/feature-x: ahead 1] WIP search
Filtering branches
Show only merged or unmerged branches
# Branches whose tip is already in the current branch git branch --merged # Useful: branches you can safely delete # Branches NOT yet merged into the current one git branch --no-merged # Compared to a different branch git branch --merged main git branch --no-merged main
Pattern matching
# Branches starting with 'feature/' git branch --list 'feature/*' # Branches starting with 'feature/' (remote too) git branch -a --list '*feature/*'
Sort by date
Most recently committed first
git branch --sort=-committerdate # * main # feature-x # bugfix-42 # old-experiment # By when they were created (oldest first) git branch --sort=authordate
For a permanently nicer order, set this as a default:
git config --global branch.sort -committerdate
Custom formatting with for-each-ref
Power user: any column you want
git for-each-ref --sort=-committerdate refs/heads/ \ --format='%(refname:short) %(committerdate:short) %(authorname)' # main 2025-01-12 Alice # feature-x 2025-01-10 Bob # bugfix-42 2025-01-08 Alice
Just the current branch
git branch --show-current # main # Or the more general: git rev-parse --abbrev-ref HEAD # main
Count branches
git branch | wc -l # local git branch -r | wc -l # remote git branch -a | wc -l # everything
Branches tracking a specific remote branch
git branch -vv | grep 'origin/main' # Shows local branches that track origin/main
Reading the asterisk and brackets
* branch-name— the currently checked-out branch.+ branch-name— branch checked out in another worktree.[origin/main]— this branch tracksorigin/main.[origin/main: ahead 2]— local has 2 commits the remote does not.[origin/main: behind 3]— remote has 3 commits the local does not.[origin/main: ahead 2, behind 3]— diverged.[gone]— the tracked remote branch no longer exists; safe to delete withgit fetch --prunefirst.
Pruning stale remote-tracking refs
Remove pointers to branches deleted on the remote
git fetch --prune # or auto-prune on every fetch: git config --global fetch.prune true
git config --global alias.brs "branch -vv --sort=-committerdate"Now
git brs gives you a clean recently-used list with tracking info — a fantastic daily-driver view.git branch --merged | grep -v "\\*" every Friday afternoon. Those are branches you can probably delete to keep things tidy.