Last updated: 2026-05-18

Branch Management

Once you are creating, merging, and deleting branches regularly, you need tools to keep track of them — which ones exist, which have been merged, which still have unmerged work, and how to rename or clean them up. This is branch management: the discipline of maintaining an organised and navigable branch structure.


Listing Branches — git branch

Running git branch with no arguments gives a simple list of all local branches:

git branch
  feature-login
  hotfix
* main

The * marks the branch HEAD is currently pointing to — the branch you are on. Any commit you make right now will move this branch forward.

Listing with last commit

To see the last commit on each branch:

git branch -v
  feature-login  93b412c Add login form component
  hotfix         1fb7853 Fix broken email address
* main           7a98805 Merge branch 'feature-login'

This gives you a quick health check of where each branch stands without having to switch to it.

Listing all branches including remote-tracking

git branch -a
  feature-login
* main
  remotes/origin/main
  remotes/origin/feature-login

Remote-tracking branches are covered in full in the Remote Branches and Tracking topic.


Filtering by Merge Status

Two flags make it easy to identify which branches are safe to delete and which still have unmerged work.

--merged — branches already merged

Lists branches whose work has already been incorporated into the current branch:

git branch --merged
  feature-login
* main

Branches on this list that don't have a * are generally safe to delete — their work is already part of the current branch, so deleting them loses nothing:

git branch -d feature-login

--no-merged — branches with unmerged work

Lists branches that contain commits not yet merged into the current branch:

git branch --no-merged
  experimental

Git protects you from accidentally deleting these:

git branch -d experimental
error: The branch 'experimental' is not fully merged.
If you are sure you want to delete it, run 'git branch -D experimental'.

The -d flag refuses to delete an unmerged branch. To force the deletion — understanding that unmerged work will be lost:

git branch -D experimental

Checking merge status relative to a specific branch

By default, --merged and --no-merged show the merge state relative to your current branch. You can also check relative to any other branch without switching to it:

git branch --no-merged main        # Branches not yet merged into main
git branch --merged feature-login  # Branches already merged into feature-login

This is useful when you want to assess the state of branches from the perspective of a branch other than the one you are on.


Renaming a Branch

Renaming a local branch

git branch --move old-name new-name

This renames the branch locally. The old name is gone and the new name takes its place — all commits remain intact.

Updating the remote after a rename

Renaming a branch locally does not affect the remote. To push the renamed branch and update the remote tracking reference:

git push --set-upstream origin new-name

After this, both names exist on the remote. The old remote branch must be explicitly deleted:

git push origin --delete old-name

Renaming the default branch (main/master)

Renaming a branch that other people are actively working from — especially the default branch — requires coordination. Before renaming main or master, make sure to:

  • Inform all collaborators so they can update their local tracking references.
  • Update any CI/CD pipelines, deployment scripts, or repository settings that reference the branch by name.
  • Update any documentation that references the old name.
# Rename locally
git branch --move master main

# Push the new name to remote
git push --set-upstream origin main

# Delete the old remote branch once everyone has updated
git push origin --delete master

Deleting Branches

Branches are cheap to create and should be deleted once their purpose is served — after they are merged, their pointer is no longer needed and cleaning them up keeps the branch list readable.

Delete a merged branch (safe)

git branch -d feature-login

Git refuses if the branch is not fully merged, protecting you from accidental data loss.

Force-delete an unmerged branch

git branch -D experimental

Use this only when you are certain you want to permanently discard the unmerged work on that branch.

Delete a remote branch

git push origin --delete feature-login

This removes the branch from the remote server. Local tracking references are cleaned up with:

git fetch --prune

Inspecting a Branch

To see detailed information about a specific branch — its last commit, what it tracks, and how far ahead or behind it is relative to its upstream:

git branch -vv
  feature-login  93b412c [origin/feature-login: ahead 2] Add form validation
* main           7a98805 [origin/main] Merge branch 'feature-login'
  hotfix         1fb7853 Fix broken email address

Reading the output:

ColumnWhat It Shows
Branch nameThe local branch
Abbreviated hashThe last commit on that branch
[origin/branch]The remote-tracking branch it is set up to follow
ahead NN commits on the local branch not yet pushed
behind NN commits on the remote not yet pulled

Note that git branch -vv shows the state as of the last time you fetched from the remote. It does not contact the network — run git fetch --all first if you want up-to-date tracking information.


Keeping the Branch List Clean

In active repositories, the branch list can grow quickly. A few practices keep it manageable:

  • Delete merged branches promptly. After a pull request or merge is complete, delete the branch immediately. Use git branch --merged to identify candidates regularly.
  • Use descriptive names. Names like feature/user-authentication or fix/login-redirect are easier to scan than my-branch or test2.
  • Prune stale remote-tracking references. After remote branches are deleted, their local tracking references linger. Clean them up with git fetch --prune or configure Git to prune automatically:
git config --global fetch.prune true

Summary

CommandWhat It Does
git branchLists all local branches
git branch -vLists branches with their last commit
git branch -vvLists branches with upstream tracking info
git branch -aLists all branches including remote-tracking
git branch --mergedLists branches merged into the current branch
git branch --no-mergedLists branches with unmerged work
git branch --merged <branch>Lists branches merged into a specific branch
git branch -d <name>Deletes a merged branch (safe)
git branch -D <name>Force-deletes a branch regardless of merge status
git branch --move <old> <new>Renames a branch locally
git push origin --delete <name>Deletes a remote branch
git fetch --pruneRemoves stale remote-tracking references