Git
table of contents
Delete merged branches
Leftovers from merged branches are distracting. Too many of them make commands like git branch -l useless
. Here are some commands to clean them up.
Get the list of all branches that have been merged into the current branch.
git branch --merged
Delete all branches that have been merged into the current branch.
- We exclude asterisk because that’s how we identify the current branch.
git_main_branch
is an Oh My Zsh function that returns the name of the main branch.
git branch --merged "$(git_main_branch)" | grep -v "$(git_main_branch)" | grep -v '*' | xargs -n 1 git branch -d
See if there is anything left among unmerged branches.
git branch --no-merged
Then delete them one by one.
git branch -D <branch_name>
Delete the refs to branches that don’t exist anymore.
git remote prune origin