Sometimes local branches can pile up in your Git repository, making it more difficult to keep track of active work. You might have a bunch of old feature branches. You might have bug-fix branches that were merged into your main branches, such as master or develop. They were never removed afterward. Over time, this leads to clutter, so it’s a good habit to clean things up periodically.
A quick way to remove those unwanted branches is to run:
git branch --merged | egrep -v "(^\*|master|develop)" | xargs git branch -d
This command looks for branches that are already merged into your current branch. It filters out the ones you want to keep, like master and develop. Then, it deletes the rest. If you’re curious about which branches would be removed but not ready to delete anything yet, you can list them first by running:
git branch --merged | egrep -v "(^\*|master|develop)"
That way, you get a preview and can confirm you’re not deleting a branch that still holds important changes. Keep in mind that if a branch was merged somewhere else, Git might warn you or refuse to remove it. If it has unmerged commits, it might require additional checks or a forced deletion.
In general, though, these commands make it easy to keep your local repository organized. They leave you free to focus on new features and improvements.