Understanding Git Branch Cleanup
After working with Git for a while, your local repository can accumulate many branches that have already been merged or no longer exist on the remote. Learning how to clean up these branches is essential for maintaining a tidy workspace.
When you work with Git branches, several scenarios can leave you with unnecessary local branches:
- Merged branches: Feature branches that were merged and are no longer needed
- Deleted remote branches: Branches that were deleted on the remote but still exist locally
- Orphaned tracking branches: Local branches tracking remotes that no longer exist
- Stale references: Local references to remote branches that have been removed
How to Delete Git Branches That Have Been Merged Manually
1. Update Your Local References
First, fetch the latest changes and prune branches that no longer exist on the remote:
git fetch --pruneThis removes "stale" remote-tracking branches (e.g., origin/feature-old) that no longer exist in the remote repository.
2. How do I delete local branches already merged?
List local branches that are already merged into your current branch (usually main):
git branch --merged⚠️ Warning: This list will include your current branch — do not delete it.
3. How to delete all merged local branches except main
To delete git branches that have been merged into main:
git branch --merged main | grep -v 'main' | xargs git branch -dReplace main with master or your default branch name.
• -d deletes the branch only if it is fully merged
• Use -D to force delete (not recommended unless you are sure)
4. How to Delete Branches That Don't Exist on Remote
To delete branches that don't exist on remote:
5. Complete Branch Cleanup Workflow
This command fetches/prunes remote branches first, then deletes local merged branches:
git fetch --prune && git branch --merged main | grep -v 'main' | xargs git branch -dCommon Use Cases
Automatically removes the feature branch that was just merged.
Perfect for regular maintenance to keep your local repository clean.
Advanced Branch Cleanup Techniques
Delete Branches by Pattern
git branch | grep 'feature/' | xargs git branch -dDelete Branches Older Than a Date
git for-each-ref --format='%(refname:short) %(committerdate)' refs/heads/ | awk '$2 < "2024-01-01" { print $1 }' | xargs git branch -dCheck Which Branches Are Safe to Delete
Safety Guidelines for Branch Cleanup
Frequently Asked Questions
How do I delete branches that have been merged?
Use `git branch --merged main | grep -v 'main' | xargs git branch -d` or simply run `gat clear`.
How do I delete branches not on remote?
First run `git fetch --prune`, then use `git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -D`.
How do I delete branches merged into master?
Replace `main` with `master` in the commands: `git branch --merged master | grep -v 'master' | xargs git branch -d`.
How do I delete branches that are deleted on remote?
Use `git fetch --prune` to update your local references, then delete the orphaned local branches.
Common Branch Cleanup Scenarios
Scenario 1: After Feature Merge
After your feature branch is merged via pull request:
Scenario 2: Cleaning Up After Team Changes
When team members delete branches on remote:
Scenario 3: Bulk Cleanup of Merged Branches
Cleaning up all merged feature branches:
git branch --merged main | grep 'feature/' | xargs git branch -dWhy Use gat clear to Delete Branches That Have Been Merged?
Instead of running multiple commands to delete git branches that have been merged, branches not on remote, or branches no longer on remote, gat clear does it all automatically:
gat clearThis single command will:
- ✅ Fetch and prune remote references
- ✅ Delete branches that have been merged into your default branch
- ✅ Delete branches not on remote that were removed upstream
- ✅ Delete branches that don't exist on remote anymore
- ✅ Safely clean up your local repository
Safety Features When Deleting Git Branches:
- ✅ Never deletes your current branch or protected branches like main/master
- ✅ Only deletes fully merged branches by default
- ✅ Shows what will be deleted before performing the action
- ✅ Respects git's merge detection to ensure no work is lost
What gat clear does for you:
- ✅ Comprehensive cleanup - Handles merged branches, orphaned branches, and stale references automatically
- ✅ Safe by default - Never deletes your current branch or protected branches
- ✅ Smart detection - Automatically identifies which branches can be safely removed
- ✅ Respects Git rules - Uses Git's merge detection to ensure no work is lost
- ✅ Handles all scenarios - Merged branches, deleted remotes, and orphaned tracking in one command
- ✅ Error prevention - Reduces risk of accidentally deleting the wrong branches