---
type: snippet
id: RYVuj6j1rJ49DePN
title: Delete All Local Branches Except main
url: >
  https://www.jakubpelak.com/snippets/delete-all-local-branches-except-main
section: snippets
tags:
  - Git
  - Automation
  - CLI
published_at: 2025-06-26
---

# Delete All Local Branches Except main

A handy one-liner to clean up all your local Git branches except `main`. It uses `-D` to **force-delete** each branch, even if it hasn’t been merged—so be careful. If you want a safer version, replace `-D` with `-d` to only delete branches that have been merged.

```sh
git branch | grep -v "main" | xargs git branch -D
```

Credit: (link: https://coderwall.com/p/x3jmig/remove-all-your-local-git-branches-but-keep-master)
