How to Delete Merged Branches in Git
- View the Existing Branches in Git
- Delete Multiple Local Merged Branches at Once in Git
- Delete Local and Remote Merged Branches in Git
In Git, branching is necessary to work with many team members. Branches represent snapshots of the changes that have been made in the past in Git.
When we make a new branch in Git, it is meant to enclose the changes we made to fix the bugs or add new features.
Through Git, we can easily manage branches, whether to create them or delete them using commands. Deleting branches is not an exception in Git.
Many developers feel satisfaction when their Git repository is clean by deleting the merged branches, and they are easy to go for the new development to the existing branches.
Let’s look at removing branches that have already been merged using the following commands in Git.
View the Existing Branches in Git
Before we start deleting the existing branches in a repository, let’s have a look at all the list of branches by running the following command:
git branch
To see the list of all the remote branches, we will run the following command:
git branch -r
To view the list of all branches (local and remote), we will run the following command:
git branch -a
As a result of these commands, we will see a list of branches. Not all the branches that we can see should be deleted. Our target is to clean the branches that are already merged.
To see the list of all local merged branches, we will run the following command:
git branch --merged
To view the list of all local unmerged branches, we will run the following command:
git branch --no-merged
Delete Multiple Local Merged Branches at Once in Git
Deleting branches one at a time is much more time-consuming and not the right job. Of course, we are developers, and we don’t prefer to do a repetitive task when we can robotize it using some commands.
As we have seen in the list of merged branches, there may be some branches that we do not want to delete in the future. So for this purpose, we will add a few arguments with the command to delete all the merged branches to exclude the branches that we don’t want to delete.
The following command will not include the master
branch, and anything that has dev in it:
git branch --merged| egrep -v "(^\*|master|main|dev)"
If we want to skip any other branch, we will add the name as follows:
git branch --merged| egrep -v "(^\*|master|main|dev|skip_branch_name)"
Now, suppose we want to delete the existing local branches of the repository already merged in the past into the currently checked-out branch of the repository. In that case, we will execute the following command:
git branch --merged | grep -i -v -E "master|dev"| xargs git branch -d
Delete Local and Remote Merged Branches in Git
To delete a merged local branch, we will run the git branch
command with the option -d
as mentioned below.
git branch -d <branch-name>
If the local branch is not merged, then we will run the following command:
git branch -D <branch-name>
If we want to delete it from the remote, we will execute the git push
command with the option --delete
(here, the name of the remote is the origin, which is by default) :
git push --delete origin <branch-name>
Alternatively, we can also use the following command:
git push origin :<branch-name>
After we have deleted the branch from the remote, we will prune to eliminate remote-tracking branches with the following command:
git remote prune origin
Individually, we can also prune remote-tracking branches with the following command:
git branch -dr <branch-name>