↑ ↓ select, Enter open, Esc close

Delete local branches that are already merged

Caution: This command changes the system. Understand what it does before you run it.

Adjust the values, the command updates live
user@server
git branch --merged main | grep -vE '^[*+]|^\s*(main|main|master|develop)$' | xargs -r git branch -d

Determines all local branches whose commits are already contained in the main branch, filters out the currently checked out branch and the usual main branches, and deletes the rest. Since git branch -d only removes fully merged branches, no work is lost. Remote branches remain untouched.

Note: Run the first part up to | xargs on its own beforehand to check the list. git fetch --prune removes stale remote-tracking branches.

Also searched as

  • git delete all merged branches
  • clean up old local branches
  • too many local branches

Related one-liners

All in Git

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.