# Branch Cleanup ## Quick flow ([[Oh-My-Zsh Git Aliases|Oh-My-Zsh]]) First, prune stale remote-tracking refs: ```bash git fetch --prune ``` Then use the oh-my-zsh aliases: | Alias | Action | |-------|--------| | `gbg` | List local branches marked as "gone" from remote | | `gbgd` | Delete them (soft — fails if unmerged) | | `gbgD` | Delete them (force) | That's usually all you need. ## Manual equivalents **List gone branches:** ```bash LANG=C git branch -vv | grep ": gone\]" ``` **Delete gone branches (soft):** ```bash LANG=C git branch --no-color -vv | grep ": gone\]" | cut -c 3- | awk '{print $1}' | xargs git branch -d ``` **Delete gone branches (force):** ```bash LANG=C git branch --no-color -vv | grep ": gone\]" | cut -c 3- | awk '{print $1}' | xargs git branch -D ``` ## Other useful commands ```bash # Delete a single local branch git branch -d branchname # soft (fails if unmerged) git branch -D branchname # force # Delete a remote branch git push --delete origin branchname # Delete all merged local branches (except main/dev) git branch --merged | grep -Ev "(^\*|master|main|dev)" | xargs git branch -d ``` [source](https://stackoverflow.com/questions/6127328/how-do-i-delete-all-git-branches-which-have-been-merged) · [source](https://stackoverflow.com/a/44643891/4700312)