Over time, your Git repository may accumulate old or unused branches. Cleaning them up helps keep your project organized and focused.
🛠️ Command to Delete a Local Branch
git branch -d branch-name
- This deletes the local branch only if it has already been merged.
- Replace
branch-name
with the name of the branch you want to delete.
✅ Example
git branch -d feature/login
This deletes the feature/login
branch if it has been merged into your current branch.
⚠️ Force Delete an Unmerged Branch
If the branch has not been merged, use -D
(capital D) to force delete:
git branch -D branch-name
Example:
git branch -D feature/experimental
⚠️ Warning: This permanently deletes the branch locally. Changes not pushed or merged will be lost.
📋 Check Available Local Branches
Before deleting, you can list all local branches:
git branch
🔄 Optional: Remove Remote-Tracking Branches Too
If you want to delete a local reference to a remote branch (after it has been deleted on the server):
git fetch -p
This prunes stale branches that no longer exist on the remote.
🧠 Summary Table
Command | Purpose |
---|---|
git branch -d branch-name | Delete merged local branch |
git branch -D branch-name | Force delete unmerged local branch |
git branch | List all local branches |
git fetch -p | Prune deleted remote-tracking branches |