← Blog

Git: Delete merged branches

January, 2020

Problem

I build an app and use git branches.

Branches sum up, I don’t know which branches are already merged into master and I lose overview.

git branch

Solution (Linux)

I need a command that checks if a branch is merged into master and if it is merged, it should get deleted locally.

  1. git checkout master (or whatever branch you want to compare to)
  2. git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Explanation

  1. git branch --merged: List all branches whose tips are reachable from the specified commit (HEAD if not specified).
  2. egrep -v "(^\*|master|dev)": Search for all lines that do not start with * (= the current branch), or don’t have master or dev in it.
  3. xargs git branch -d: Execute the command to delete all found branches from step 2.

Further Reading


miku86

Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.