How to remove a local Git branch

Introduction
In this small article we'll look into the steps you can follow to delete a local Git branch.
Steps
To remove a local Git branch, follow these steps:
- Switch to a different branch: You cannot delete a branch you are currently on. Use
git checkout <another_branch_name>orgit switch <another_branch_name>to move to a different branch (e.g.,mainordevelop).
git checkout main
Delete the local branch:
- If the branch has been fully merged into your current branch: Use the
-d(or--delete) flag. This is the safe way to delete branches as it prevents data loss from unmerged changes.git branch -d <branch_to_delete> - If the branch has unmerged changes (force delete): Use the
-Dflag. This will delete the branch even if it contains commits not merged into any other local branches or pushed to a remote. Use this with caution as it can lead to data loss if not handled carefully.git branch -D <branch_to_delete>
- If the branch has been fully merged into your current branch: Use the
Verify the deletion: You can check if the branch has been successfully removed from your local repository by listing all local branches.
git branchThe deleted branch should no longer appear in the list.
Conclusion
And there you have it, a simply step by step guide to help you manage your Git branches.




