Delete all branches except main / master
Deleting all branches of a Git project can be a useful task when you want to simplify the history of a project or when you need to maintain only the main branch. Fortunately, Git provides an easy way to do this.
First, to ensure that we have a backup of all our branches, we must make a local backup of our repository. Then, we can use the following command in the terminal to delete all branches except the main one:
git branch | grep -v "main" | xargs git branch -D
This command lists all the branches of the project, excluding the main branch, and deletes them. This way, only the main branch will be kept in the project.
It’s important to note that once the branches are deleted, they cannot be recovered, so make sure to backup before running this command.
Useful?
Yes, deleting all branches of a Git project is a useful task in specific situations, such as when you need to simplify the project history or when you need to maintain only the main branch. Although it’s not something that is done regularly, it can be an effective solution in cases where the repository has become disorganized and needs to be cleaned up.
In summary, it’s a technique that is used occasionally and in specific situations.