Cleaning up old or unused branches is a good Git practice to keep your workspace organized. Git provides simple commands to safely remove local branches that you no longer need.
🧭 1. List All Local Branches
First, see your local branches:
git branch
Example output:
main
* feature/login
bugfix/header
The
*indicates your current branch. You cannot delete the branch you’re currently on.
🗑️ 2. Delete a Local Branch
To delete a local branch (e.g., feature/login), run:
git branch -d feature/login
-ddeletes the branch only if it has been fully merged into your current branch or another target branch.
❗ 3. Force Delete (if Not Merged)
To delete a branch regardless of its merge status (use with caution):
git branch -D feature/login
-Dis a shortcut for--delete --force
⚠️ Safety Check
Always make sure the branch is no longer needed and that any important changes are merged or pushed before deleting.
📌 Summary
| Task | Command |
|---|---|
| List local branches | git branch |
| Delete merged branch | git branch -d branch-name |
| Force delete unmerged branch | git branch -D branch-name |