Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
When working on a project with Git, it's common to create and merge branches. However, over time, these branches can accumulate and clutter your local repository. Deleting merged branches is a good practice to keep your repository clean and organized.
Delete Merged Branches
Your task is to delete all local branches that have been merged into the master branch of the https://github.com/labex-labs/git-playground repository.
- Change to the repository directory:
cd git-playground
- List all local branches that have been merged into
master:
git branch --merged
Output:
* master
new-branch
new-branch-1
new-branch-2
new-branch-3
- Delete all merged branches:
git branch --merged master | awk '!/^[ *]*$/ && !/master/ {print $1}' | xargs git branch -d
- List all branches again:
git branch
This is the final result:
* master
Summary
Deleting merged branches is a good practice to keep your local repository clean and organized. Use the git branch --merged <branch> command to list all branches merged into <branch>, and the git branch -d <branch> command to delete a branch. Remember to always be careful when deleting branches, as you may lose important work if you delete the wrong branch.