How to Delete All Local Branches in Git?

Git provides a powerful version control system that allows developers to collaborate efficiently on projects. Over time, however, a repository can have a lot of local branches that are no longer needed, cluttering the workspace and potentially causing confusion.

The “main” is the by default branch created in all git repositories. All the other branches can be merged into the main branch. Deleting all local branches after merging them into the main branch allows us to keep the repository clean. 

Steps to Delete All Local Branches in Git

Step 1: Check the list of branches.

Open the GitHub repository’s local directory in your git bash. This repository contains the branches that you want to delete. For example, we have created a test repository and this repository contains 4 local branches and a main branch.

git branch

git branches

Step 2: Switch to the main branch

Our task is to delete all local branches and keep the main branch safe. Before running the command to delete, make sure you are switched to the main branch and all the changes are safely merged. To switch to the main branch, open up the editor’s terminal and type the following command:

git checkout main

git checkout main

Step 3: Delete all the remaining branches

The following command will delete all local branches and keep the main branch safe. Run the following command in your terminal to see the changes:

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

The command to delete all local branches except the main branch involves several Git utilities combined in a single line.

  • git branch: This command lists all the local branches in the repository.
  • grep -v "main": This is a filter command that excludes the main branch from the list. The -v option inverts the match, so it selects all lines that do not contain the specified pattern, which in this case is “main”.
  • xargs git branch -D: xargs is a command that takes input from standard input and converts it into arguments for another command. In this case, it takes the list of branch names (excluding the main branch) and passes them as arguments to git branch -D, which deletes each branch.

Deleted branches

Step 4: Now if you check branches, you will only find main.

git branches after deletion

Safety Measures

Before executing the command, it’s essential to understand its implications and potential risks:

  • Double-check: Ensure that you are in the correct Git repository directory before running the command. Deleting branches is irreversible, so it’s important to verify that you are targeting the correct repository.
  • Backup: If you’re unsure about deleting branches, consider creating a backup of your repository or making a copy of the branches you want to keep.
  • Review the Branch List: Before executing the command, review the list of branches that will be deleted to confirm that it includes only the branches you intend to remove.
  • Main Branch Naming: Depending on your repository configuration, the main branch may be named master, main, or something else. Adjust the command accordingly to match the name of your main branch.

Benefits of Cleaning Up Branches

  • Improved Clarity: Removing unnecessary branches reduces clutter and makes it easier to navigate the repository.
  • Reduced Risk: Fewer branches mean fewer potential points of confusion or conflicts, reducing the risk of errors during development.
  • Enhanced Performance: A cleaner repository can improve Git’s performance, especially when dealing with large projects or repositories with extensive branch histories.
  • Simplified Collaboration: Maintaining a clean repository helps collaboration by ensuring that team members can easily understand the current state of the codebase.

Contact Us