Delete merged branches

Git, Repository, Branch · Apr 13, 2021

Deletes all local merged branches.

  • Use git branch --merged <branch> to list all branches merged into <branch>.
  • Use the pipe operator (|) to pipe the output and grep -v "(^\*|<branch>)" to exclude the current and the target <branch>.
  • Use the pipe operator (|) to pipe the output and xargs git branch -d to delete all of the found branches.
git branch --merged <branch> | grep -v "(^\*|<branch>)" | xargs git branch -d
git checkout master
git branch
# master
# patch-1
# patch-2

# Assuming `patch-1` is merged into master
git branch --merged master | grep -v "(^\*|master)" | xargs git branch -d

git branch
# master
# patch-2

More like this

  • Git Branches

    Master git branch commands with this snippet collection of simplified git documentation and tips.

    Collection · 38 snippets

  • View merged branches

    Prints a list of all merged local branches.

    Git, Repository · Apr 13, 2021

  • Delete detached branches

    Deletes all detached branches.

    Git, Repository · Apr 13, 2021

  • Delete a branch

    Deletes a local branch.

    Git, Repository · Apr 13, 2021