How to delete local branches that have been merged

Greg Foster
Greg Foster
Graphite software engineer

After merging feature branches into the main branch, it's often a good practice to delete these branches to reduce clutter and minimize confusion. This guide will show you how to identify and delete local branches that have been merged into your main branch.

Before deleting branches, you first need to know which branches have been merged into your current branch (typically the main or master branch). This is a safe practice to ensure you do not accidentally delete branches that are still in use.

Command to List Merged Branches:

Terminal
git checkout main
git branch --merged

This command switches you to the main branch and then lists all local branches that have been merged into main. The output will include main itself and any feature branches that have been fully merged.

Once you've identified the merged branches, you can proceed to delete them. It's important to ensure you are not currently on a branch that you want to delete. Git will not allow you to delete the branch you are currently on.

Command to delete a specific merged branch:

Terminal
git branch -d <branch-name>

This command deletes the specified branch, using -d which ensures that the branch is fully merged before deletion. If you try to delete a branch that hasn't been merged, Git will prevent this action to avoid losing work.

If you want to clean up multiple branches that have been merged, you can automate the process with a simple command. This is useful after a major release or cleanup session.

Command to delete all merged branches except main:

Terminal
git branch --merged | grep -v "^\*\\|main" | xargs -n 1 git branch -d

This command does the following:

  • git branch --merged: Lists all merged branches.
  • grep -v "^\*\\|main": Filters out the main branch and the current branch (marked with an asterisk).
  • xargs -n 1 git branch -d: Passes each branch name to git branch -d to delete it.

Once you've merged these branches and deleted them locally, you might also want to clean them up on the remote.

Command to Delete a Remote Branch:

Terminal
git push origin --delete <branch-name>

This command tells Git to delete the specified branch from the remote repository named origin.

When you delete local branches that have already been merged into the main branch (usually main or master), Git does not automatically remove the corresponding remote tracking branches from your local repository. These remote tracking branches are references to branches on the remote repository and can become stale if the corresponding branches on the remote have been deleted or renamed.

You can do this by running:

Terminal
git remote prune origin

By pruning remote branches, you ensure that your local repository accurately reflects the state of the remote repository. This helps avoid confusion and prevents outdated references from cluttering your local branch list. Keeping the remote branch list updated facilitates smoother collaboration with other team members, as everyone is working with consistent and up-to-date information.

  • Always ensure that a branch is fully merged before deleting it. The -d option in the git branch command helps protect against accidental deletion of unmerged branches.
  • It's a good idea to review branches manually or with your team before removing them, especially in a collaborative environment.

For further reading see the official Git documentation.

On this page
Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2