While working on Git, we usually merge different branches for multiple operations and features. This platform also permits you to create new branches if required and can switch to any existing branch whenever needed. By default, you work in the root branch of the project, known as the “main” branch.
However, there exists a situation when you may want to prune unnecessary branches from the Git repository to free up the workspace and be more organized. To do so, utilize the “$ git branch -D <branchname>” command in the Git bash terminal.
This manual will explain how to prune merged and unmerged local branches in Git.
How to Prune Local Unmerged Branches in Git?
Let’s consider that we have a project with multiple branches created on the local machine for different purposes, but they do not exist in the remote repositories. Now, we want to clean the Git local repository. Before doing that, it is required to execute the “$ git branch -a” command to check all branches that are available in our repository and then remove them using the “$ git branch -D <branchname>” command.
Now, move ahead toward the stated procedure implementation!
Step 1: Move to Git Directory
First, navigate to the Git directory using the “cd” command:
Step 2: List All Branches
To list all branches, we have used the “-a” flag with the “git branch” command:
As you can see, we have multiple local branches and the symbol “*” beside the “main” branch which indicates that this is our current working branch. Moreover, the last three branches are the cloned remote branches:
Step 3: Prune Non-fully Local Branch
To prune or clean the non-fully, unmerged local branch, execute the given command with the “-D” option abbreviated for “-force -delete”:
Below output indicates that our branch “alpha” is deleted successfully from the project root directory:
Step 4: Verify Prune Operation
In order to verify that the “alpha” branch is pruned or not, execute the “git branch” command:
As you can see, the deleted branch is nowhere in the branches list:
Want to prune merged local branches? Check out the following section!
How to Prune Merged Local Branches in Git?
A merged branch is a type of branch that is pulled and merged with the remote repository branch. Git provides the “git branch -d <branch-name>” command to prune the selected merged local branch.
The below steps will lead you to perform the above action!
Step 1: Prune Merged Local Branch
Run the provided command and specify the branch name with the “-d” option:
Here, our specified local merged branch “mybranch” is permanently deleted from the project root directory:
Step 2: Verify Prune Operation
Now, execute the “git branch” with “-a” option to list the all branch and verify the deleting action:
The below-given output indicates that “mybranch” merged branch no longer exists in the repository:
We have provided the instructions related to prune local branches in Git.
To prune local branches in Git, first, navigate to the Git local repository. Then, list all present branches on the current repository. After that, run the “$ git branch -D <branch-name>” command to delete the unmerged local branch. To prune the merged branch, execute the “$ git branch -d <branch-name>” command. This manual demonstrated how to prune local branches in Git.