Deleting Local Git Branches
Deleting local Git branches is a straightforward process that can be accomplished using a few simple commands. Here's how you can delete local branches:
Listing Local Branches
To view a list of all the local branches in your repository, use the following command:
git branch
This will display all the local branches, including the currently active branch (indicated by an asterisk).
Deleting a Single Branch
To delete a single local branch, use the git branch -d
command followed by the branch name:
git branch -d <branch-name>
This command will delete the specified branch, as long as it has been fully merged into another branch.
Deleting an Unmerged Branch
If you attempt to delete a branch that has not been merged, Git will refuse to do so by default. To force the deletion of an unmerged branch, use the -D
option instead of -d
:
git branch -D <branch-name>
This will delete the branch regardless of its merge status.
Batch Deletion of Branches
To delete multiple local branches at once, you can use the following command:
git branch | grep -v "master" | xargs git branch -d
This command will delete all local branches except the master
branch (or the main branch in your repository). You can modify the grep -v "master"
part to exclude different branches as needed.
By using these commands, you can efficiently manage and clean up your local Git branches, keeping your repository organized and streamlined. In the next section, we will explore the process of deleting remote Git branches.