How to Delete All Local Git Branches

🧹 Cleaning your local machine and keeping only current branches leads to free space on your disk and fewer problems due to old stuff

Riccardo Giorato
Geek Culture

--

https://unsplash.com/photos/MAYEkmn7G6E

Why should I clean/delete them locally?

Cleaning your local branches ensures:
1. Using less space on your device
2. Prevent Errors when sending local branches to remote(you won’t push to the remote old branches from months ago you forgot)
3. You can clearly visualize which are the branches you are working on.
4. You keep the central git repository as the source of all possible active branches( if it sounds risky maybe you just need to have a backup of the branches off your git hosting provider )

Delete all local branches except for “main

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

Explanation:

  • 🛒 Get all branches (with the exception of the main branch) via git branch | grep -v "main" command;
  • 👇 Select every branch with xargs command;
  • 🔥 Delete branch with xargs git branch -D

--

--