To delete all local Git branches, you can use command-line tools. Here is a detailed explanation of the steps and commands:
First, open your terminal or command prompt. Then, use the following command to view all local branches:
bashgit branch
This command lists all local branches. To delete all local branches except the current one, use the following command:
bashgit branch | grep -v "^*" | xargs git branch -D
The working principle of this command is as follows:
git branchlists all branches.grep -v "^*"filters out the current branch (which is marked with an asterisk).xargs git branch -Dpasses the filtered branch names to thegit branch -Dcommand to force delete them.
Please note that this command deletes all branches except the current one, including those that may not have been merged. Before executing this operation, ensure you have saved all important changes and confirm if you really want to delete these branches.
If you only want to delete a specific branch, use the following more direct command:
bashgit branch -D branch-name
Replace "branch-name" with the actual name of the branch you want to delete.
In my experience, I once needed to batch delete multiple test branches that were no longer needed and had been merged into the main branch. Using the above batch deletion command, I was able to quickly clean up these unnecessary branches, keeping the branch list clean and manageable. This is very helpful for maintaining a clear project structure.