diff --git a/bin/git-delete-gone-branches b/bin/git-delete-gone-branches index 1cfadc7..d3dafd2 100755 --- a/bin/git-delete-gone-branches +++ b/bin/git-delete-gone-branches @@ -3,6 +3,8 @@ set -euo pipefail dry_run=false +force=false +prune=false while [[ $# -gt 0 ]]; do case "$1" in @@ -10,15 +12,28 @@ while [[ $# -gt 0 ]]; do 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]" + echo "Usage: git delete-gone-branches [-n|--dry-run] [-f|--force] [-p|--prune]" exit 1 ;; esac done -gone_branches=$(git branch -vv | awk '/: gone\]/{print $1}') +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') if [ -z "$gone_branches" ]; then echo "No branches with a gone remote found." @@ -31,4 +46,15 @@ if [ "$dry_run" = true ]; then 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 diff --git a/man/git-delete-gone-branches.1 b/man/git-delete-gone-branches.1 new file mode 100644 index 0000000..ea121cd --- /dev/null +++ b/man/git-delete-gone-branches.1 @@ -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 <\fIutsav\.sabharwal@sysdig\.com\fR> +. +.SH "REPORTING BUGS" +<\fIhttps://github\.com/tj/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttps://github\.com/tj/git\-extras\fR> diff --git a/man/git-delete-gone-branches.html b/man/git-delete-gone-branches.html new file mode 100644 index 0000000..0b62d27 --- /dev/null +++ b/man/git-delete-gone-branches.html @@ -0,0 +1,135 @@ + + +
+ + +
+ git-delete-gone-branches - Delete branches whose remote is gone
+
git-delete-gone-branches [-n|--dry-run] [-f|--force] [-p|--prune]
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.
+ +-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.
$ git delete-gone-branches
+
+$ git delete-gone-branches --dry-run
+
+$ git delete-gone-branches --force
+
+$ git delete-gone-branches --prune
+
+
+Written by Utsav Sabharwal <utsav.sabharwal@sysdig.com>
+ +<https://github.com/tj/git-extras/issues>
+ +<https://github.com/tj/git-extras>
+ + +