Skip to main content
  1. blog/

Delete Local Branches in Git

··2 mins

Overview #

Branch management in Git is an important skill for developers. Git provides simple yet powerful tools to manage branches, including deleting those that are no longer needed.

In this article, I’ll demonstrate how to delete multiple local branches in Git. The commands shown work in Linux and MacOS.


Delete local branches merged to develop #

$ git checkout develop
$ git branch | grep -v develop | xargs git branch -d
  • git branch: Lists all local branches.

  • grep -v develop: Filters out the develop branch from the list. The -v option inverts the match, meaning it selects all lines except those containing develop.

  • xargs git branch -d: Takes the filtered list of branches and applies git branch -d to each. The -d flag deletes branches that have been merged to current branch.

This command is safe for cleaning up branches after merging their changes into the develop branch. It ensures that you don’t accidentally delete branches with work that hasn’t been merged.


Delete all local branches #

$ git branch | xargs git branch -d
  • git branch: Lists all local branches.

  • xargs git branch -d: Takes the filtered list of branches and applies git branch -d to each. The -d flag deletes branches that have been merged to current branch.

This command is more aggressive and should be used with caution. It’s suitable for scenarios where you want to completely clean your local repository, removing all branches except for the one you’re currently on.