Skip to main content

How to Delete Fully Merged Branches

Short answer

A local branch that has been fully merged into another branch can be safely deleted with git branch -d <branch-name>; Git refuses the deletion if the branch contains unmerged commits, protecting you from data loss. To delete all local branches that have already been merged into main in one pass, run: git branch --merged main | grep -v '\*\|main' | xargs git branch -d. For remote branches, delete them individually with git push origin --delete <branch-name>, or enable automatic deletion in GitHub's repository Settings under 'Automatically delete head branches' so merged PR branches are removed immediately. After collaborators delete their remote branches, everyone should run git fetch --prune to clean up their local stale remote-tracking references. Regularly pruning merged branches keeps the branch list small, reduces confusion about what is actively in development, and avoids the 'branch graveyard' problem that accumulates in long-lived repositories.

In a hurry? Watch our brief 1-minute vertical video that summarizes the info below.

Over time, your local repository can get cluttered with old branches. To keep things tidy, it's a good practice to delete branches that have been fully merged into your main branch.

Deleting All Merged Branches at Once

First, make sure you are on your main branch:

git checkout main

Then, you can run this command to find and delete all local branches that have been merged into main:

git branch --merged | egrep -v "(^\*|main|dev)" | xargs git branch -d

Let's break down this command:

  • git branch --merged: This lists all branches that have been fully merged into your current branch (which should be main).
  • egrep -v "(^\*|main|dev)": This filters the list. The -v flag inverts the match, so it removes the current branch (*), the main branch, and a dev branch from the list. You should adjust this part to fit your branching workflow, protecting any other long-running branches you might have.
  • xargs git branch -d: This takes the filtered list of branches and, for each one, runs the git branch -d command to delete it.

Deleting a Single Branch

If you just want to delete a single local branch, you can use the -d (or --delete) flag:

git branch -d feature/new-login

This is a "safe" operation: Git will prevent you from deleting a branch if it has changes that haven't been merged yet.

To force the deletion of a branch, you can use the -D flag (a capital D). Be careful, as this will discard any unmerged work on that branch.

Careful: this command can discard work.

git branch -D feature/new-login

The Git Cheat Sheet

No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!

Free. We email you a ZIP with our cheat sheets.

Deleting Remote Branches

Keep in mind that these commands only delete local branches. To delete a branch from a remote repository, you need to use the git push command with the --delete flag:

git push origin --delete feature/new-login
No Command Line Needed

Cleaning Up Branches in Tower

The Tower Git client offers a dedicated "Branches Review" feature. You can filter your branches to show only the "Fully Merged" ones and delete them with a single click.

Learn More

Free download

Download the Cheat Sheet

Get 18 of our most popular Cheat Sheets in one handy ZIP!