Delete all local branches in git

Created at 20-Nov-2023 , By samar

Here's how you can delete all local branches, excluding the one you want to retain, promoting a cleaner repository. 

To delete all branches in a Git repository, you can use the following commands.

Delete local branches except main in GIT

Delete all local branches except the local main branch.

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

Delete all other local branches except the main and branch-to-keep branch

You can change the name of branch-to-keep branch with another existing local branch which you don’t want to delete from your local branches.

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

 

 

 

 

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.