General

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:

txt
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:


Terminal
git branch

output
 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):


Terminal
git branch -a

output
 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:


Terminal
git branch -r

To see the latest commit on each branch, add -v:


Terminal
git branch -v

output
 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:


Terminal
git branch -vv

output
 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:


Terminal
git branch feature/login

To start the branch from a specific commit, tag, or another branch, pass it as the second argument:


Terminal
git branch hotfix v1.4.0


Terminal
git branch hotfix 3d4e5f6

Creating 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:


Terminal
git switch -c feature/login

The 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:


Terminal
git branch -m new-name

To rename any branch, pass both the old and the new name:


Terminal
git branch -m old-name new-name

If 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:


Terminal
git branch -d feature/login

output
Deleted branch feature/login (was 3d4e5f6).

Git refuses to delete a branch that has unmerged work, to protect you from losing commits:

output
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:


Terminal
git branch -D feature/wip

You 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:


Terminal
git branch --merged

This is the safest way to find branches that can be deleted without losing history. Before deleting anything, preview the filtered list:


Terminal
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:


Terminal
git branch --merged | grep -vE '^*|^[[:space:]]*(main|develop)$' | xargs -r git branch -d

To list branches that still have unmerged work, use --no-merged:


Terminal
git branch --no-merged

These 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:


Terminal
git branch --merged origin/main

This 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):


Terminal
git branch --set-upstream-to=origin/main main

output
Branch 'main' set up to track remote branch 'main' from 'origin'.

To remove tracking information from a branch, use --unset-upstream:


Terminal
git branch --unset-upstream main

When 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:


Terminal
git push -u origin feature/login

Showing the Current Branch

To print just the name of the branch you are on, use --show-current:


Terminal
git branch --show-current

output
main

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:


Terminal
git branch -c old-branch new-branch

The 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:


Terminal
git branch --sort=-committerdate

The 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.

AI Assistant
Context loaded: git branch Command: Create, List, and Delete Branches