git branch Command: Create, List, and Delete Branches
Branches are how Git keeps your work separated from the main line of development. A new branch lets you experiment, fix a bug, or build a feature without touching what is already shipped, and a merge brings the work back together when it is ready. The tool that creates, lists, and removes those branches is git branch.
This guide explains how to use git branch to manage local and remote branches, filter by merged or upstream state, rename branches, and clean up stale ones.
git branch Syntax
The general form of the command is:
git branch [OPTIONS] [BRANCH] [START_POINT]With no arguments, git branch lists your local branches and marks the currently checked-out one with an asterisk. With a branch name, it creates a new branch pointing at the current commit. With additional flags, it can list remote branches, delete branches, rename them, or set upstream tracking.
Listing Branches
To list the local branches in the current repository, run git branch with no arguments:
git branch develop
* main
feature/login
The asterisk marks the branch you are currently on. The branches are sorted alphabetically.
To include remote-tracking branches, add -a (all):
git branch -a develop
* main
feature/login
remotes/origin/HEAD -> origin/main
remotes/origin/develop
remotes/origin/main
Anything prefixed with remotes/ is a local reference to a branch on a remote, not a branch you can commit to directly. Check it out to create a local tracking branch.
To list only remote-tracking branches, use -r:
git branch -rTo see the latest commit on each branch, add -v:
git branch -v develop a1b2c3d Add release notes
* main 7e8f9a0 Merge pull request #42
feature/login 3d4e5f6 Stub login form
For even more detail, -vv also prints the upstream branch each local branch tracks and whether it is ahead, behind, or in sync:
git branch -vv develop a1b2c3d [origin/develop] Add release notes
* main 7e8f9a0 [origin/main] Merge pull request #42
feature/login 3d4e5f6 [origin/feature/login: ahead 2] Stub login form
Creating a Branch
To create a new branch, pass the name as an argument. This creates the branch at the current HEAD but does not switch to it:
git branch feature/loginTo start the branch from a specific commit, tag, or another branch, pass it as the second argument:
git branch hotfix v1.4.0git branch hotfix 3d4e5f6Creating a branch without switching to it is useful when you want to mark a commit for later work. Most of the time, though, you create a branch and switch to it in one step. The modern command for that is git switch -c:
git switch -c feature/loginThe older equivalent is git checkout -b feature/login. Both create the branch at the current commit and check it out immediately. See the create and list Git branches
guide for a fuller walkthrough.
Renaming a Branch
To rename the branch you are currently on, use -m with the new name:
git branch -m new-nameTo rename any branch, pass both the old and the new name:
git branch -m old-name new-nameIf a branch with the target name already exists, this form fails to protect you from overwriting it. To force the rename and overwrite the existing branch, use the capital -M instead. Use -M with care: it discards the destination branch.
After renaming a branch that is already pushed, push the new name to the remote and delete the old one so collaborators see the change. The rename a Git branch guide walks through the full local-and-remote flow.
Deleting a Branch
To delete a local branch that has been merged into the current branch, use -d:
git branch -d feature/loginDeleted branch feature/login (was 3d4e5f6).
Git refuses to delete a branch that has unmerged work, to protect you from losing commits:
error: The branch 'feature/wip' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/wip'.
If you are sure the work is no longer needed, force the deletion with -D:
git branch -D feature/wipYou cannot delete the branch you are currently on. Switch to another branch first, then delete it. For remote branch deletion, see the delete local and remote Git branch guide.
Filtering by Merged State
git branch can filter branches by whether they are fully merged into the current branch. List branches that have been merged:
git branch --mergedThis is the safest way to find branches that can be deleted without losing history. Before deleting anything, preview the filtered list:
git branch --merged | grep -vE '^\*|^[[:space:]]*(main|develop)$'The grep filter keeps the current branch and the protected main and develop branches out of the list. If the output looks correct, pipe the same list to git branch -d:
git branch --merged | grep -vE '^\*|^[[:space:]]*(main|develop)$' | xargs -r git branch -dTo list branches that still have unmerged work, use --no-merged:
git branch --no-mergedThese are the branches you should review before deleting, because they contain commits that are not on your current branch.
Filtering Remote Branches Too
--merged and --no-merged accept a reference argument. To check against main from the remote:
git branch --merged origin/mainThis is useful in CI or cleanup scripts where you want to see which local branches are safely merged into the shipped code, regardless of which branch you are currently on.
Setting an Upstream Branch
An upstream branch tells Git which remote branch your local branch tracks for git pull and git push. To set it on an existing branch, use --set-upstream-to (or -u):
git branch --set-upstream-to=origin/main mainBranch 'main' set up to track remote branch 'main' from 'origin'.
To remove tracking information from a branch, use --unset-upstream:
git branch --unset-upstream mainWhen you push a new branch to a remote for the first time, pass -u to git push to create the remote branch and set it as the upstream in one step:
git push -u origin feature/loginShowing the Current Branch
To print just the name of the branch you are on, use --show-current:
git branch --show-currentmain
This is the right command for shell prompts and scripts. It prints nothing if you are in a detached HEAD state, which scripts can detect and handle.
Copying a Branch
git branch -c copies a branch, including its reflog and configuration, to a new name:
git branch -c old-branch new-branchThe original branch is kept. Use the capital -C to force the copy and overwrite an existing target branch. Copying is less common than creating a fresh branch, but it is useful when you want to preserve the history and config of a branch under a new name.
Sorting the Branch List
By default, branches are listed alphabetically. The --sort option changes the order. To sort by the date of the most recent commit:
git branch --sort=-committerdateThe minus sign reverses the order, so the most recently updated branch appears first. This is handy when you return to a project after a break and want to see what you were working on last.
Quick Reference
| Command | Description |
|---|---|
git branch |
List local branches |
git branch -a |
List local and remote-tracking branches |
git branch -r |
List only remote-tracking branches |
git branch -v |
Show the latest commit on each branch |
git branch -vv |
Show upstream tracking and ahead/behind counts |
git branch NAME |
Create a new branch at HEAD
|
git branch NAME START |
Create a branch at a specific commit or tag |
git branch -m NEW |
Rename the current branch |
git branch -m OLD NEW |
Rename another branch |
git branch -d NAME |
Delete a merged branch |
git branch -D NAME |
Force-delete a branch with unmerged work |
git branch --merged |
List branches merged into the current branch |
git branch --no-merged |
List branches with unmerged work |
git branch -u origin/main |
Set the upstream of the current branch |
git branch --show-current |
Print the current branch name |
git branch --sort=-committerdate |
Sort by most recent commit |
FAQ
What is the difference between git branch and git checkout?git branch manages branches: creating, listing, renaming, and deleting. git checkout (and its modern replacement git switch) changes which branch you are on. Creating and switching in one step uses git switch -c or git checkout -b.
How do I delete a remote branch?git branch -d only deletes local branches. To delete a branch on the remote, run git push origin --delete BRANCH_NAME. The delete local and remote Git branch
guide covers the full flow, including cleanup of stale tracking references.
Why does Git say “branch is not fully merged”?
The branch has commits that are not reachable from your current branch. Git is protecting you from losing work. Review the commits with git log BRANCH_NAME, merge them if needed, and then use -d again. If you are certain the commits are not needed, force the delete with -D.
How do I see which branch a remote branch tracks locally?
Run git branch -vv. The output includes each local branch’s upstream in brackets along with its ahead and behind counts relative to that upstream.
Can I list branches sorted by who committed most recently?
Yes. Use git branch --sort=-committerdate to sort branches by the timestamp of their most recent commit, with the freshest branch first. This is a fast way to see what is actively being worked on.
Conclusion
git branch covers the full lifecycle of a branch: creating it, renaming it, keeping track of what it points at, and deleting it when the work is done. Combined with git switch for moving between branches and git merge for integrating changes, it is the foundation of any Git workflow.
For related Git commands, see the git diff
guide for reviewing changes and the git log
guide for inspecting branch history.
