diff --git a/Commands.md b/Commands.md index 295f71ec..e9af2477 100644 --- a/Commands.md +++ b/Commands.md @@ -18,6 +18,7 @@ - [`git cp`](#git-cp) - [`git create-branch`](#git-create-branch) - [`git delete-branch`](#git-delete-branch) + - [`git delete-gone-branches`](#git-delete-gone-branches) - [`git delete-merged-branches`](#git-delete-merged-branches) - [`git delete-squashed-branches`](#git-delete-squashed-branches) - [`git delete-submodule`](#git-delete-submodule) @@ -932,6 +933,21 @@ Delete local and remote tag `name`: $ git delete-tag 0.0.1 ``` +## git delete-gone-branches + +Deletes all local branches whose remote-tracking branch has been deleted (i.e. branches shown as `[gone]` in `git branch -vv`). Use `--dry-run` to preview what would be deleted. + +```bash +$ git delete-gone-branches +Deleted branch feature-123 (was abc1234). +Deleted branch feature-456 (was def5678). + +$ git delete-gone-branches --dry-run +Would delete the following branches: + feature-123 + feature-456 +``` + ## git delete-merged-branches Deletes branches that are listed in `git branch --merged`. diff --git a/bin/git-delete-gone-branches b/bin/git-delete-gone-branches new file mode 100755 index 00000000..beb2a543 --- /dev/null +++ b/bin/git-delete-gone-branches @@ -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') + +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 diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index 128d269b..9b95c585 100644 --- a/etc/git-extras-completion.zsh +++ b/etc/git-extras-completion.zsh @@ -213,6 +213,11 @@ _git-delete-branch() { __gitex_branch_names_unique } +_git-delete-gone-branches() { + _arguments \ + '(-n --dry-run)'{-n,--dry-run}'[show branches that would be deleted without deleting]' +} + _git-delete-squashed-branches() { _arguments \ ':branch-name:__gitex_branch_names' @@ -421,6 +426,7 @@ zstyle ':completion:*:*:git:*' user-commands $existing_user_commands \ count:'show commit count' \ create-branch:'create branches' \ delete-branch:'delete branches' \ + delete-gone-branches:'delete branches whose remote is gone' \ delete-merged-branches:'delete merged branches' \ delete-squashed-branches:'delete squashed branches' \ delete-submodule:'delete submodules' \ diff --git a/man/git-delete-gone-branches.1 b/man/git-delete-gone-branches.1 new file mode 100644 index 00000000..dfd67b12 --- /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 <\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> diff --git a/man/git-delete-gone-branches.html b/man/git-delete-gone-branches.html new file mode 100644 index 00000000..0b62d271 --- /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>
+ + +