How to Delete Already Merged Git Branches
Know how to delete already merged git branches to keep your repository clean and easy to browse and how to exclude branches you do not want to delete.
Deleting merged branches is something that many developers like to do to keep their git repository clean and easy to browse. Let's find out how to remove branches that have already been merged.
Steps to deleting already merged branches
Here are the steps you need to take and get the job done properly:
Listing branches
List all the branches that were merged into the remote.
list merged branches in the remote git
git branch -r --mergedThere may be some branches that you do not want to delete. Thus, we add a few arguments to exclude master and develop branches. The following command will skip the <kbd class="highlighted">master</kbd> branch, and anything that includes <kbd class="highlighted">dev</kbd> in it:
skip the master branch git
git branch -r --merged | grep -i -v -E "master|dev"If you want to exclude another branch, add the name to the <kbd class="highlighted">grep</kbd> sequence like this:
exclude branch git
git branch -r --merged | grep -i -v -E "master|dev|skip-branch-name"To delete all local branches that are already merged into the currently checked out branch:
delete all local branches git
git branch --merged | grep -i -v -E "master|dev" | xargs -d '\n' git branch -dDeleting local and remote branches
Note: You cannot delete the currently checked-out branch. Switch to another branch first.
Delete a merged local branch by running the git branch command with the <kbd class="highlighted">-d</kbd> option. For more detailed information and instructions, you can check out How to Delete Both Local and Remote Branches in Git.
delete a merged local branch git
git branch -d <branch-name>If it's not merged, run:
delete a not merged local branch git
git branch -D <branch-name>Delete it from the remote by the git push command with <kbd class="highlighted">--delete</kbd> (suppose, the name of remote is origin, which is by default) :
git push --delete origin branchname
git push --delete origin <branch-name>As an alternative, you can use the following command:
delete it from the remote git
git push origin :<branch-name>Deleting remote-tracking branches
After deleting the branch from the remote, prune to get rid of remote-tracking branches with the git remote command:
delete remote-tracking branches git
git remote prune originor prune individual remote-tracking branches with the <kbd class="highlighted">git branch -dr</kbd> command:
delete individual remote tracking branches git
git branch -dr <branch-name>Branching
Branches are a pointer to a snapshot of the changes. A new branch is created to encapsulate the changes when you want to fix bugs or add new features helping you to clean up the history before merging it. Branches represent an isolated line of development. They are accepted as a way to request a new working directory, staging area, and project history. Developing isolated lines of development for two features in branches will enable developers to operate on them in parallel and make the master branch free from questionable code.
Using the 'git-delete-merged-branches' tool
Alternatively, you can use the git-delete-merged-branches tool (https://github.com/hartwork/git-delete-merged-branches) to automate the process of deleting merged branches. This tool will list all merged branches and prompt you to select which ones you want to delete. Here's how to use it:
- Install the
git-delete-merged-branchestool using one of the methods described in the README file on GitHub. - Make sure you are on the branch you want to delete:
git checkout [branch-name] - Run the
git-delete-merged-branchescommand:git-delete-merged-branches - Follow the prompts to select which merged branches you want to delete.
This tool can save time if you have many merged branches to delete. However, note that it may not be suitable for all use cases, and it's always a good idea to make a backup of your repository before making any major changes.