Skip to content

Instantly share code, notes, and snippets.

@Kenny2github
Created July 6, 2022 16:31
Show Gist options
  • Save Kenny2github/fdc1d1532894dc98f05940d38f2ac578 to your computer and use it in GitHub Desktop.
Save Kenny2github/fdc1d1532894dc98f05940d38f2ac578 to your computer and use it in GitHub Desktop.
A git script to batch delete branches by glob pattern.
@echo off
if "%1"=="" (
echo usage: git del-branches ^<glob pattern^>
) else (
for /f "usebackq" %%b in (`git branch --list %1`) do (
git branch -d %%b
)
)
#!/bin/sh
if [ -z "$1" ]; then
echo 'usage: git del-branches "<glob pattern>"'
else
git branch --list "$1" | sed 's/^* //' | xargs -r git branch -d
fi

Installation

Windows

  1. Save git-del-branches.cmd somewhere.
  2. Add it as an alias: git config --global alias.del-branches "!C:\\absolute\\path\\to\\git-del-branches.cmd"
  3. Usage: git del-branches glob*pattern to delete all branches starting with glob and ending with pattern

Linux

  1. Save git-del-branches.sh somewhere.
  2. Make it executable: chmod a+x git-del-branches.sh
  3. Add it as an alias: git config --global alias.del-branches "!/absolute/path/to/git-del-branches.sh"
  4. Usage: git del-branches "glob*pattern" to delete all branches starting with glob and ending with pattern (quotes necessary otherwise glob is expanded by shell instead of git)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment