A Zsh script to clean up local and remote Git branches that have already been rebased (fully merged) onto a target branch.
Depends on branch name prefixes; for example, feature/
, bugfix/
, etc. I tend to use my username boneskull
.
- Deletes local branches matching a prefix that have been rebased onto a target branch.
- Deletes remote branches matching a prefix that have been rebased onto a target branch.
- Prompts for confirmation before deleting any branch.
- Deletes local branches that track remote branches that have been deleted (via
git fetch --prune
); not limited to branches matching the prefix! - Automatically detects the default branch (probably)
git-cleanup [remote] [target] [prefix]
remote
: The remote repository (default:origin
)target
: The target branch to check against (default: remote's default branch)prefix
: The prefix for branches to consider (default:user.branchPrefix
config orboneskull
)
git cleanup origin main feature
This will clean up all local and remote branches starting with feature/
that have been rebased onto origin/main
.
- Zsh (tested w/ v5.9)
- Git (tested w/ v2.49.0)
You probably want to set the default branch prefix in your Git config:
git config --global user.branchPrefix 'some-prefix'
Put git-cleanup.zsh
in your PATH
, but rename it to git-cleanup
; it will now be available as a git command (git cleanup
). Ensure it is executable (chmod +x git-cleanup
).
- The methods used here were adapted from StackOverflow posts.
- I stuffed the config in
user.branchPrefix
but it may belong somewhere else. - The script is not compatible with Bash.
- Copilot wrote most of this README.
- Always review the branches to be deleted when prompted!
- I don't know how this handles merge commits. It probably doesn't.
- Please comment if something is broken!
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to https://unlicense.org/