Deleting Branches
Once a feature is merged or a branch is no longer needed, you should delete it to keep your repository clean.
Delete Local Branches
Safe Delete (-d)
bash
1# Delete a merged branch
2git branch -d branch-nameGit will only delete the branch if it has been merged:
bash
1git branch -d feature-complete
2# Deleted branch feature-complete (was abc1234).If not merged, Git will warn you:
bash
1git branch -d unmerged-branch
2# error: The branch 'unmerged-branch' is not fully merged.
3# If you are sure you want to delete it, run 'git branch -D unmerged-branch'.Force Delete (-D)
bash
1# Force delete (even if not merged)
2git branch -D branch-name⚠️ Warning: This permanently deletes the branch and its unique commits!
bash
1git branch -D abandoned-feature
2# Deleted branch abandoned-feature (was def5678).Delete Remote Branches
Method 1: git push --delete
bash
1git push origin --delete branch-nameMethod 2: git push with colon
bash
1git push origin :branch-nameExample
bash
1# Delete remote branch
2git push origin --delete feature-old
3
4# Output:
5To github.com:user/repo.git
6 - [deleted] feature-oldDelete Both Local and Remote
bash
1# Delete local
2git branch -d feature-complete
3
4# Delete remote
5git push origin --delete feature-completeClean Up Stale Remote References
After remote branches are deleted, clean up local references:
bash
1# Prune stale remote-tracking branches
2git fetch --prune
3
4# Or prune for specific remote
5git remote prune originSee Stale Branches
bash
1# Show remote-tracking branches that no longer exist on remote
2git branch -vv | grep ': gone]'Cannot Delete Current Branch
You cannot delete the branch you're currently on:
bash
1git checkout feature-xyz
2git branch -d feature-xyz
3# error: Cannot delete branch 'feature-xyz' checked outSolution: Switch to another branch first:
bash
1git checkout main
2git branch -d feature-xyz
3# Deleted branch feature-xyzDelete Multiple Branches
One by One
bash
1git branch -d branch1 branch2 branch3Using Pattern (with grep)
bash
1# Delete all feature branches
2git branch | grep 'feature/' | xargs git branch -dDelete All Merged Branches
bash
1# Delete all merged branches except main
2git branch --merged | grep -v "main" | xargs git branch -dBest Practices
1. Merge Before Deleting
bash
1# Ensure branch is merged
2git checkout main
3git merge feature-complete
4git branch -d feature-complete2. Verify Before Force Delete
bash
1# Check what commits would be lost
2git log main..unmerged-branch
3
4# If you're sure
5git branch -D unmerged-branch3. Clean Up Regularly
bash
1# Weekly cleanup script
2git fetch --prune
3git branch --merged | grep -v "main" | xargs git branch -dCommon Scenarios
Delete Feature After PR Merge
bash
1# After PR is merged on GitHub
2git checkout main
3git pull origin main
4git branch -d feature-complete
5git fetch --pruneDelete Abandoned Experiment
bash
1# Force delete abandoned work
2git branch -D experiment-failedClean Up After Sprint
bash
1# Delete all merged feature branches
2git branch --merged main | grep -v "main" | xargs git branch -dSummary
| Task | Command |
|---|---|
| Safe delete (merged) | git branch -d name |
| Force delete | git branch -D name |
| Delete remote | git push origin --delete name |
| Prune stale | git fetch --prune |
Practice deleting branches in the next exercise!
