feat: add git-delete-gone-branches command#1239
feat: add git-delete-gone-branches command#1239codersofthedark wants to merge 3 commits intotj:mainfrom
Conversation
Adds a new command to delete local branches whose remote-tracking branch has been deleted (shown as [gone] in git branch -vv). This is a common need after PRs are merged and remote branches are cleaned up, leaving stale local branches behind. Features: - Deletes all local branches with a gone remote in one command - --dry-run / -n flag to preview branches before deletion Closes tj#1220
| esac | ||
| done | ||
|
|
||
| gone_branches=$(git branch -vv | awk '/: gone\]/{print $1}') |
There was a problem hiding this comment.
git branch -vv output includes the latest commit subject. If a commit message contains the literal text : gone], this branch would be incorrectly matched.
What about
gone_branches=$(git for-each-ref --format
'%(if:equals=[gone])%(upstream:track,nobracket)%(then)%(refname:short)%(end)' refs/heads/ | sed '/^$/d')
There was a problem hiding this comment.
Good catch — switched to git for-each-ref with %(upstream:track,nobracket) to match only the tracking status, so commit message content can't cause false positives.
| exit 0 | ||
| fi | ||
|
|
||
| echo "$gone_branches" | xargs git branch -D |
There was a problem hiding this comment.
Could we prompt for the branch deletion by default?
There was a problem hiding this comment.
Added an interactive [y/N] confirmation prompt by default. Use -f/--force to skip it (consistent with how git-clear and git-sync handle this in the repo).
| exit 1 | ||
| ;; | ||
| esac | ||
| done |
There was a problem hiding this comment.
Do we need to run git fetch --prune before?
There was a problem hiding this comment.
Added a -p/--prune flag that runs git fetch --prune before checking. Kept it opt-in rather than automatic to avoid unexpected network calls.
- Use git for-each-ref instead of git branch -vv to avoid false positives from commit messages containing ': gone]' - Add interactive confirmation prompt by default; skip with -f/--force - Add -p/--prune flag to run git fetch --prune before checking - Add generated man page .1 and .html files
|
@spacewander Do you think we should have a policy about AI contributions? In my opinion, it's highly disrespectful if I spent my time reviewing code, and the reply from the contributor is fully AI generated. I'm not against the usefulness of AI, I use it quite frequently, but I think we should draw the line somewhere as maintainers. I hear many other open-source projects are figuring stuff out as well. With the goal of allowing contributors to use AI, but still require talking to an actual human, I'm thinking about some ground-rules like:
What do you think? |
|
@hyperupcall |
| fi | ||
|
|
||
| gone_branches=$(git for-each-ref --format \ | ||
| '%(if:equals=[gone])%(upstream:track,nobracket)%(then)%(refname:short)%(end)' refs/heads/ | sed '/^$/d') |
There was a problem hiding this comment.
with ,nobracket, I think you'll want the if-condition to be:
%(if:equals=gone)
(remove the square brackets)
@codersofthedark
Closes #1220
What
Adds a new
git delete-gone-branchescommand that deletes all local branches whose remote-tracking branch has been deleted (shown as[gone]ingit branch -vv).Why
After PRs are merged and remote branches are cleaned up on GitHub/GitLab, local branches are left behind with no remote. There's no existing git-extras command to clean these up —
git delete-merged-brancheshandles a different case (merged but remote still exists).Usage
Changes
bin/git-delete-gone-branches— the commandman/git-delete-gone-branches.md— man pageCommands.md— added to index and description sectionetc/git-extras-completion.zsh— zsh completion with--dry-runflagChecklist
#!/usr/bin/env bashCommands.mdetc/git-extras-completion.zshcheck_integrity.sh— requires local install of ronn to generate.1/.htmlman files; happy to add if needed