- Published on
Git delete all branches except main
- Authors

- Name
- Gene Zhang
git branch | grep -v "main" | xargs git branch -D
This command will delete all branches except main:
git branch: Lists all local branches in your repository.grep -v "main": Filters out the line containing "main".-vinverts the match, so branches named main are excluded from the output.xargs git branch -D: Takes the list of branches (excluding main) and passes each branch name as an argument to the git branch -D command.git branch -Dforcibly deletes a branch, even if it hasn’t been fully merged.