Deleting a Local Git Branch
Listing Local Branches
Before deleting a local branch, it's important to first list all the existing local branches in your Git repository. You can do this using the git branch
command:
git branch
This will display a list of all the local branches in your repository, with the currently checked-out branch marked with an asterisk (*
).
Deleting a Local Branch
To delete a local Git branch, you can use the git branch -d <branch-name>
command. For example, to delete a branch named feature/new-functionality
, you would run:
git branch -d feature/new-functionality
If the branch has already been merged into another branch, Git will allow you to delete it. However, if the branch has not been merged, Git will refuse to delete it by default to prevent accidental data loss. In this case, you can use the -f
or --force
option to delete the branch forcefully:
git branch -f feature/new-functionality
Be cautious when using the force option, as it can lead to data loss if the branch contains important changes that have not been merged.
Deleting Multiple Local Branches
If you need to delete multiple local branches, you can use the git branch -d
command with a space-separated list of branch names:
git branch -d feature/new-functionality feature/bug-fix
Alternatively, you can use the git branch -D
command to delete multiple branches, including those that have not been merged:
git branch -D feature/new-functionality feature/bug-fix
Again, use caution when deleting unmerged branches, as you may lose important changes.