Skip to content

Instantly share code, notes, and snippets.

@pnhoang
Last active August 6, 2018 08:38
Show Gist options
  • Save pnhoang/bdbfbeea76d7db4fb796029f01c17375 to your computer and use it in GitHub Desktop.
Save pnhoang/bdbfbeea76d7db4fb796029f01c17375 to your computer and use it in GitHub Desktop.
[To delete all local branches that are already merged into the currently checked out branch:](https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged)
```git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d```
explain:
- git branch --merged: get all merged branch, this may include master or dev branch that you want to keep
- the second command, egrep -v, will look for all these branches and invert the search with a regular expression to search for master or dev branch
- all the results are listed on multiple lines, we need them as input parameters to the next command, we use xargs. The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard
input and executes utility with the strings as arguments.
- then we can delete them with git branch -d as usual.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment