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(1) - Delete branches whose remote is gone + + + + +
+ + + +
    +
  1. git-delete-gone-branches(1)
  2. +
  3. Git Extras
  4. +
  5. git-delete-gone-branches(1)
  6. +
+ +

NAME

+

+ git-delete-gone-branches - 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 <utsav.sabharwal@sysdig.com>

+ +

REPORTING BUGS

+ +

<https://github.com/tj/git-extras/issues>

+ +

SEE ALSO

+ +

<https://github.com/tj/git-extras>

+ + +
    +
  1. +
  2. March 2026
  3. +
  4. git-delete-gone-branches(1)
  5. +
+ +
+ + diff --git a/man/git-delete-gone-branches.md b/man/git-delete-gone-branches.md index 5ca4f8d..c5fc432 100644 --- a/man/git-delete-gone-branches.md +++ b/man/git-delete-gone-branches.md @@ -3,13 +3,14 @@ git-delete-gone-branches(1) -- Delete branches whose remote is gone ## SYNOPSIS -`git-delete-gone-branches` [-n|--dry-run] +`git-delete-gone-branches` [-n|--dry-run] [-f|--force] [-p|--prune] ## DESCRIPTION - Deletes all local branches whose remote-tracking branch has been deleted - (i.e. branches marked as `[gone]` in `git branch -vv`). This commonly - happens after a pull request is merged and the remote branch is removed. + 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 @@ -17,12 +18,25 @@ git-delete-gone-branches(1) -- Delete branches whose remote is gone 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 <> diff --git a/man/git-extras.md b/man/git-extras.md index 89f05f3..efdf1ca 100644 --- a/man/git-extras.md +++ b/man/git-extras.md @@ -46,6 +46,7 @@ git-extras(1) -- Awesome GIT utilities - **git-cp(1)** Copy a file keeping its history - **git-create-branch(1)** Create branches - **git-delete-branch(1)** Delete branches + - **git-delete-gone-branches(1)** Delete branches whose remote is gone - **git-delete-merged-branches(1)** Delete merged branches - **git-delete-squashed-branches(1)** Delete branches that were squashed - **git-delete-submodule(1)** Delete submodules diff --git a/man/index.txt b/man/index.txt index 4f24ad0..77ec05c 100644 --- a/man/index.txt +++ b/man/index.txt @@ -18,6 +18,7 @@ git-count(1) git-count git-cp(1) git-cp git-create-branch(1) git-create-branch git-delete-branch(1) git-delete-branch +git-delete-gone-branches(1) git-delete-gone-branches git-delete-merged-branches(1) git-delete-merged-branches git-delete-squashed-branches(1) git-delete-squashed-branches git-delete-submodule(1) git-delete-submodule