mirror of
https://github.com/tj/git-extras.git
synced 2026-09-11 16:06:20 -04:00
feat: add git-delete-gone-branches command
Adds a new command to delete local branches whose remote-tracking branch has been deleted (shown as [gone] in git branch -vv). This is a common need after PRs are merged and remote branches are cleaned up, leaving stale local branches behind. Features: - Deletes all local branches with a gone remote in one command - --dry-run / -n flag to preview branches before deletion Closes #1220
This commit is contained in:
parent
65dd36ae62
commit
79ba015412
16
Commands.md
16
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`.
|
||||
|
|
|
|||
34
bin/git-delete-gone-branches
Executable file
34
bin/git-delete-gone-branches
Executable file
|
|
@ -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
|
||||
|
|
@ -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' \
|
||||
|
|
|
|||
36
man/git-delete-gone-branches.md
Normal file
36
man/git-delete-gone-branches.md
Normal file
|
|
@ -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 <<utsav.sabharwal@sysdig.com>>
|
||||
|
||||
## REPORTING BUGS
|
||||
|
||||
<<https://github.com/tj/git-extras/issues>>
|
||||
|
||||
## SEE ALSO
|
||||
|
||||
<<https://github.com/tj/git-extras>>
|
||||
Loading…
Reference in a new issue