-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Problem Statement
When working with Git repositories, local branches often accumulate after their remote tracking branches have been deleted (typically after PRs are merged and branches are cleaned up on GitHub/GitLab). Currently, git-extras provides git-delete-merged-branches to clean up merged branches, but there's no command to clean up branches whose remote tracking branch no longer exists.
Developers commonly encounter branches marked as [gone] when running git branch -vv:
$ git branch -vv
feature-123 abc1234 [origin/feature-123: gone] Fix bug
feature-456 def5678 [origin/feature-456: gone] Add feature
* main ghi9012 [origin/main] Latest commitCurrently, users must manually identify and delete these branches, or write custom scripts/aliases.
Proposed Solution
Add a new command: git delete-gone-branches (or git prune-branches)
Basic Usage:
# Delete all local branches whose remote tracking branch is gone
$ git delete-gone-branches
# Preview mode (show what would be deleted without actually deleting)
$ git delete-gone-branches --dry-runExpected Behavior:
- Run
git fetch --pruneto update remote tracking information - Identify all local branches where the remote tracking branch is marked as
gone - Display the list of branches to be deleted
- Delete the branches (or just show them if
--dry-runis used)
Implementation Details
The core logic would be similar to this bash script:
#!/usr/bin/env bash
# Fetch and prune remote tracking branches
git fetch --prune
# Find branches marked as 'gone'
gone_branches=$(git branch -vv | awk '/: gone]/{print $1}')
if [ -z "$gone_branches" ]; then
echo "No branches with gone remotes found."
exit 0
fi
# Show branches
echo "The following branches will be deleted:"
echo "$gone_branches"
# Delete branches
echo "$gone_branches" | xargs git branch -DUse Cases
- Post-PR cleanup: After PRs are merged and remote branches deleted, clean up local workspace
- Repository maintenance: Keep local branch list clean and manageable
- CI/CD workflows: Automated cleanup in CI environments
- Team workflows: Standardize branch cleanup across development teams
Alternatives Considered
- git-trim (standalone tool) - Exists but requires separate installation
- Custom aliases - Works but not standardized or discoverable
- Manual deletion - Error-prone and time-consuming
Having this as part of git-extras would:
- Provide a standardized, well-tested solution
- Make it easily discoverable alongside
git-delete-merged-branches - Integrate with existing git-extras ecosystem
Additional Features (Optional)
--dry-runflag for preview mode-for--forceflag to skip confirmation- Exclude specific branches via pattern matching
- Interactive mode with confirmation prompts
Related Commands
This would complement the existing:
git delete-merged-branches- Deletes merged branchesgit prune- Prunes unreachable objects (different use case)
References
- Common workflow: https://dev.to/stephdotnet/clean-your-local-git-branches-2e69
- Similar discussion: https://stackoverflow.com/questions/7726949/remove-tracking-branches-no-longer-on-remote
I'm happy to contribute this feature if the maintainers are interested. Let me know if you'd like any clarifications or have suggestions for the implementation approach!