How to Remove Unwanted Branches from Local Using Git Bash

Introduction

When working on tech projects, developers often find themselves with numerous deleted branches accumulated over time. These branches are typically leftover from features that have been merged into the main codebase and then discarded, but they persist in the developer's local and remote repositories. This accumulation can clutter the repository and make it difficult to manage branches effectively.

This guide provides a streamlined approach to removing all deleted branches, decluttering your repository, and improving overall project management.

Remove unwanted local branches using Git Bash

Before we start with the process, open Git Bash and make sure you are in the desired repository from which you want to remove unwanted branches.

First, it's imperative to remove branches that are no longer associated with the remote repository from our local 'remote.' To achieve this, execute the following command in Git Bash.

git remote prune origin

Once all your local remote is synched with the git remote, now it is time to delete all the unwanted local branches. To do this, run the following command.

git fetch -p && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

Once you are done, running both of the commands, you will see a list of branches that got deleted/removed from your local.

Conclusion

Mastering the art of removing unwanted branches from your local repository using Git Bash is a valuable skill for any developer. By following the steps outlined in this guide, you can effectively declutter your local repository, improve project organization, and ensure a seamless workflow. Embracing these practices not only enhances your productivity but also contributes to the overall cleanliness and manageability of the project. With a clear understanding of how to remove unwanted branches, you can streamline your development process and maintain a well-organized repository, ultimately leading to more efficient and effective software development.