diff --git a/Commands.md b/Commands.md index 295f71e..e9af247 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 0000000..1cfadc7 --- /dev/null +++ b/bin/git-delete-gone-branches @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +set -euo pipefail + +dry_run=false + +while [[ $# -gt 0 ]]; do + case "$1" in + -n|--dry-run) + dry_run=true + shift + ;; + *) + echo "Unknown option: $1" + echo "Usage: git delete-gone-branches [-n|--dry-run]" + exit 1 + ;; + esac +done + +gone_branches=$(git branch -vv | awk '/: gone\]/{print $1}') + +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 "$gone_branches" | xargs git branch -D diff --git a/etc/git-extras-completion.zsh b/etc/git-extras-completion.zsh index 128d269..9b95c58 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.md b/man/git-delete-gone-branches.md new file mode 100644 index 0000000..5ca4f8d --- /dev/null +++ b/man/git-delete-gone-branches.md @@ -0,0 +1,36 @@ +git-delete-gone-branches(1) -- Delete branches whose remote is gone +==================================================================== + +## SYNOPSIS + +`git-delete-gone-branches` [-n|--dry-run] + +## 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. + +## OPTIONS + + -n, --dry-run + + Show the branches that would be deleted without actually deleting them. + +## EXAMPLES + + $ git delete-gone-branches + + $ git delete-gone-branches --dry-run + +## AUTHOR + +Written by Utsav Sabharwal <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<>