Command

// gat clear
branch cleanup

Automatically delete git branches that have been merged, are no longer on remote, or don't exist on remote anymore. Keep your repository clean and organized.

Basic Usage
gat clear

Delete all branches that have been merged into the main branch

// Video Short

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 --prune

This 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 -d

Replace 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:

git remote prune origin
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -D

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 -d

Common Use Cases

After a Pull Request is Merged
# After your PR is merged on GitHub
git checkout main
git pull origin main
gat clear

Automatically removes the feature branch that was just merged.

Weekly Repository Cleanup
# Clean up all stale branches
gat clear

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 -d

Delete 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 -d

Check Which Branches Are Safe to Delete

git branch --merged main
git branch -vv | grep ': gone]'

Safety Guidelines for Branch Cleanup

Always fetch first - Run `git fetch --prune` to update remote references
Check merge status - Use `git branch --merged` to verify branches are fully merged
Backup important work - Create tags or backup branches for important commits
Use -d not -D - Let Git prevent deletion of unmerged branches
Test on a clone first - Practice cleanup commands on a repository copy

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:

git checkout main
git pull origin main
git branch -d feature-branch

Scenario 2: Cleaning Up After Team Changes

When team members delete branches on remote:

git fetch --prune
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

Scenario 3: Bulk Cleanup of Merged Branches

Cleaning up all merged feature branches:

git branch --merged main | grep 'feature/' | xargs git branch -d

Why 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 clear

This 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

Instead of remembering multiple commands (git fetch --prune,git branch --merged,git branch -vv, complex awk scripts),gat clear does it all intelligently and safely.