Skip to content

Feature Request: Add git-delete-gone-branches command #1220

@codersofthedark

Description

@codersofthedark

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 commit

Currently, 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-run

Expected Behavior:

  1. Run git fetch --prune to update remote tracking information
  2. Identify all local branches where the remote tracking branch is marked as gone
  3. Display the list of branches to be deleted
  4. Delete the branches (or just show them if --dry-run is 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 -D

Use Cases

  1. Post-PR cleanup: After PRs are merged and remote branches deleted, clean up local workspace
  2. Repository maintenance: Keep local branch list clean and manageable
  3. CI/CD workflows: Automated cleanup in CI environments
  4. 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-run flag for preview mode
  • -f or --force flag 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 branches
  • git prune - Prunes unreachable objects (different use case)

References


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!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions