git branch lists, creates, renames, and deletes branches in a Git repository. Branches are just lightweight, movable pointers to commits — creating one is nearly instant because it doesn’t copy any files. This is the seventh post in this site’s Git command guide, following Git Status.
Listing Branches
Run git branch with no arguments to see your local branches:
$ git branch
* main
development
feature-123
The * marks the branch you currently have checked out — main here. The others, development and feature-123, exist locally but aren’t active right now.
Creating and Deleting Branches
To create a new branch called new-feature off your current branch:
$ git branch new-feature
Note this only creates the branch — it doesn’t switch you onto it. You’d still need git checkout new-feature (or git switch new-feature) to start working there, or skip the two-step with git checkout -b new-feature.
To delete a branch that’s already been merged:
$ git branch -d old-feature
Deleted branch old-feature (was 3f6a2c1).
If the branch has commits that haven’t been merged anywhere, -d refuses and tells you so:
$ git branch -d old-feature
error: The branch 'old-feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D old-feature'.
That message is Git protecting you, not a bug. Only reach for the capital -D once you’re sure you don’t need those commits.
Renaming a Branch: master to main
If you’re working in an older repository still on master and want to align with the modern default (see Git Init for why main became standard), renaming is a one-line local operation:
$ git branch -m master main
-m renames the branch you specify (or your current branch, if you omit the old name) while preserving its full history. Renaming locally doesn’t touch the remote — the remote still has master until you push the new name and update its default branch:
$ git push -u origin main
$ git push origin --delete master
Before deleting master on the remote, change the repository’s default branch in GitHub/GitLab settings first — otherwise open pull requests and the repo’s default view will still point at a branch that no longer exists.
Reference: Common git branch Flags
| Flag | What it does |
|---|---|
-d <branch> | Deletes a branch, but only if it’s fully merged (safe delete) |
-D <branch> | Force-deletes a branch regardless of merge status (use with care) |
-a | Lists local and remote-tracking branches together |
-r | Lists only remote-tracking branches (e.g. origin/main) |
-m <old> <new> | Renames a branch, preserving history |
-v | Shows the last commit on each branch alongside its name |
FAQs
What’s the difference between git branch -d and git branch -D?
-d checks first that the branch’s commits are reachable from another branch (usually because they were merged) before deleting. -D skips that check entirely and deletes regardless — it’s how you lose unmerged commits if you’re not careful.
Does renaming a branch locally affect collaborators?
Not until you push the rename and they pull it. Each collaborator’s local master branch persists until they manually rename or re-clone; coordinate the remote rename and tell your team to update their remotes (git fetch origin, then point their local branch at origin/main).
How do I see which branches are merged into main?
Run git branch --merged main. Anything listed there is safe to delete with -d without Git complaining.
See Also: This Site’s Git Command Guide
This post is part of a 13-part Git command guide on this site:
- Git Init — starting a brand-new repository
- Git Clone — copying an existing repository
- Git Add — staging specific files for a commit
- Git Add . (All) — staging all changes at once
- Git Commit — saving staged changes to the repository
- Git Status — checking what’s changed before you commit
- Git Checkout — switching branches and restoring files
- Git Push — sending your commits to a remote
- Git Pull — fetching and merging remote changes
- Git Log — reading commit history
- Git: Delete Last Commit — undoing a commit safely
- Git – Autocorrect Spelling — typo recovery and productivity config
Conclusion
Branches in Git are cheap by design — there’s no real cost to creating one for every feature, fix, or experiment, and -d‘s merge check means deleting them is hard to get badly wrong. The one place I’ve actually lost work was a careless -D on a branch I assumed was merged but wasn’t, so I treat that flag the way I treat rm -rf. Once you’ve got the branch you want, switching onto it is covered next in Git Checkout.