Menu
Coddy logo textTech

Git Branch

Last updated

git branch is the command for managing branches - it lists them, creates them, deletes them, and renames them. On its own it lists your local branches with the current one marked. Note that git branch feature only creates a branch; to switch to it, use git switch or git checkout.

Try these in the terminal playground - a real shell in your browser, nothing to install.

A branch is just a movable pointer to a commit - main and feature share history until they diverge.

Syntax

CommandWhat it does
git branchList local branches
git branch -aList local and remote branches
git branch featureCreate a branch (without switching)
git branch -d featureDelete a merged branch
git branch -D featureForce-delete an unmerged branch
git branch -m newnameRename the current branch
git branch --mergedList branches merged into HEAD

Common cases

GoalCommand
See all branches including remotesgit branch -a
See each branch's upstreamgit branch -vv
Clean up merged branchesgit branch --merged
Create and switch in one stepgit switch -c feature

Git branch FAQ

How do I list all branches in Git?
Run git branch to list local branches, with an asterisk marking the one you're on. Add -a (git branch -a) to include remote-tracking branches, or -r to list only remote branches. Use git branch -vv to also see each branch's upstream and ahead/behind status.
Does git branch create and switch to the branch?
No - git branch feature only creates the branch; you stay where you are. To create and switch in one step, use git switch -c feature (or the older git checkout -b feature). Plain git branch is for management, not navigation.
How do I delete a branch?
Use git branch -d <name> to delete a branch whose work is merged (Git refuses otherwise, protecting unmerged commits), or git branch -D <name> to force-delete regardless. You can't delete the branch you're currently on - switch away first.
How do I rename a branch?
Run git branch -m <newname> to rename the current branch, or git branch -m <oldname> <newname> to rename another one. If the branch was already pushed, you'll also need to update it on the remote - see the git rename branch page.
Can I practice this online?
Yes. Open the terminal playground to run git branch in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers branching step by step.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED