[Git] Delete all branches except main

2024-04-28 hit count image

Let's see how to delete all branches except main in local.

Outline

When reviewing other people’s code and developing at work, many branches are accumulated locally. At this time, you can use the deletion option (-D) of the Git branch command to delete the branch, but it is cumbersome to delete many accumulated branches one by one.

In this blog post, I will introduce how to delete all branches except the main branch using Git.

git branch command

By running the following command, you can check all branches currently in the local.

git branch

And by running the following command, you can delete a specific branch.

git branch -D [BRANCH_NAME]

Delete all branches except main branch

It is impossible to delete all branches except the main branch using only the basic commands provided by Git. To delete all branches except the main branch, you can use the following command.

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

Completed

Done! We’ve seen how to delete all branches except the main branch in Git. If you have many branches accumulated locally and it is difficult to delete them one by one, try using the above command to delete all branches except the main branch.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts