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.
Syntax
| Command | What it does |
|---|---|
git branch | List local branches |
git branch -a | List local and remote branches |
git branch feature | Create a branch (without switching) |
git branch -d feature | Delete a merged branch |
git branch -D feature | Force-delete an unmerged branch |
git branch -m newname | Rename the current branch |
git branch --merged | List branches merged into HEAD |
Common cases
| Goal | Command |
|---|---|
| See all branches including remotes | git branch -a |
| See each branch's upstream | git branch -vv |
| Clean up merged branches | git branch --merged |
| Create and switch in one step | git 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.