-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add git-delete-gone-branches command #1239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| dry_run=false | ||
| force=false | ||
| prune=false | ||
|
|
||
| while [[ $# -gt 0 ]]; do | ||
| case "$1" in | ||
| -n|--dry-run) | ||
| dry_run=true | ||
| shift | ||
| ;; | ||
| -f|--force) | ||
| force=true | ||
| shift | ||
| ;; | ||
| -p|--prune) | ||
| prune=true | ||
| shift | ||
| ;; | ||
| *) | ||
| echo "Unknown option: $1" | ||
| echo "Usage: git delete-gone-branches [-n|--dry-run] [-f|--force] [-p|--prune]" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| if [ "$prune" = true ]; then | ||
| git fetch --prune | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with |
||
|
|
||
| if [ -z "$gone_branches" ]; then | ||
| echo "No branches with a gone remote found." | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [ "$dry_run" = true ]; then | ||
| echo "Would delete the following branches:" | ||
| echo "$gone_branches" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "The following branches will be deleted:" | ||
| echo "$gone_branches" | ||
|
|
||
| if [ "$force" = false ]; then | ||
| read -r -p "Delete these branches? [y/N] " confirm | ||
| if [[ ! "$confirm" =~ ^[yY]$ ]]; then | ||
| echo "Aborted." | ||
| exit 0 | ||
| fi | ||
| fi | ||
|
|
||
| echo "$gone_branches" | xargs git branch -D | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we prompt for the branch deletion by default?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an interactive |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| .\" generated with Ronn/v0.7.3 | ||
| .\" http://github.com/rtomayko/ronn/tree/0.7.3 | ||
| . | ||
| .TH "GIT\-DELETE\-GONE\-BRANCHES" "1" "March 2026" "" "Git Extras" | ||
| . | ||
| .SH "NAME" | ||
| \fBgit\-delete\-gone\-branches\fR \- Delete branches whose remote is gone | ||
| . | ||
| .SH "SYNOPSIS" | ||
| \fBgit\-delete\-gone\-branches\fR [\-n|\-\-dry\-run] [\-f|\-\-force] [\-p|\-\-prune] | ||
| . | ||
| .SH "DESCRIPTION" | ||
| Deletes all local branches whose remote\-tracking branch has been deleted\. This commonly happens after a pull request is merged and the remote branch is removed\. By default, lists the branches and prompts for confirmation before deleting\. | ||
| . | ||
| .SH "OPTIONS" | ||
| \-n, \-\-dry\-run | ||
| . | ||
| .P | ||
| Show the branches that would be deleted without actually deleting them\. | ||
| . | ||
| .P | ||
| \-f, \-\-force | ||
| . | ||
| .P | ||
| Skip the confirmation prompt and delete branches immediately\. | ||
| . | ||
| .P | ||
| \-p, \-\-prune | ||
| . | ||
| .P | ||
| Run \fBgit fetch \-\-prune\fR before checking for gone branches, to ensure remote\-tracking refs are up to date\. | ||
| . | ||
| .SH "EXAMPLES" | ||
| . | ||
| .nf | ||
|
|
||
| $ git delete\-gone\-branches | ||
|
|
||
| $ git delete\-gone\-branches \-\-dry\-run | ||
|
|
||
| $ git delete\-gone\-branches \-\-force | ||
|
|
||
| $ git delete\-gone\-branches \-\-prune | ||
| . | ||
| .fi | ||
| . | ||
| .SH "AUTHOR" | ||
| Written by Utsav Sabharwal <\fIhttps://github\.com/codersofthedark\fR> | ||
| . | ||
| .SH "REPORTING BUGS" | ||
| <\fIhttps://github\.com/tj/git\-extras/issues\fR> | ||
| . | ||
| .SH "SEE ALSO" | ||
| <\fIhttps://github\.com/tj/git\-extras\fR> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| git-delete-gone-branches(1) -- Delete branches whose remote is gone | ||
| ==================================================================== | ||
|
|
||
| ## SYNOPSIS | ||
|
|
||
| `git-delete-gone-branches` [-n|--dry-run] [-f|--force] [-p|--prune] | ||
|
|
||
| ## DESCRIPTION | ||
|
|
||
| Deletes all local branches whose remote-tracking branch has been deleted. | ||
| This commonly happens after a pull request is merged and the remote branch | ||
| is removed. By default, lists the branches and prompts for confirmation | ||
| before deleting. | ||
|
|
||
| ## OPTIONS | ||
|
|
||
| -n, --dry-run | ||
|
|
||
| Show the branches that would be deleted without actually deleting them. | ||
|
|
||
| -f, --force | ||
|
|
||
| Skip the confirmation prompt and delete branches immediately. | ||
|
|
||
| -p, --prune | ||
|
|
||
| Run `git fetch --prune` before checking for gone branches, to ensure | ||
| remote-tracking refs are up to date. | ||
|
|
||
| ## EXAMPLES | ||
|
|
||
| $ git delete-gone-branches | ||
|
|
||
| $ git delete-gone-branches --dry-run | ||
|
|
||
| $ git delete-gone-branches --force | ||
|
|
||
| $ git delete-gone-branches --prune | ||
|
|
||
| ## AUTHOR | ||
|
|
||
| Written by Utsav Sabharwal <<https://github.com/codersofthedark>> | ||
|
|
||
| ## REPORTING BUGS | ||
|
|
||
| <<https://github.com/tj/git-extras/issues>> | ||
|
|
||
| ## SEE ALSO | ||
|
|
||
| <<https://github.com/tj/git-extras>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to run
git fetch --prunebefore?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a
-p/--pruneflag that runsgit fetch --prunebefore checking. Kept it opt-in rather than automatic to avoid unexpected network calls.