Skip to content

Instantly share code, notes, and snippets.

@guillaumerocq
Created February 12, 2024 21:30
Show Gist options
  • Save guillaumerocq/99efed8bb48d154d9ada362994af23c4 to your computer and use it in GitHub Desktop.
Save guillaumerocq/99efed8bb48d154d9ada362994af23c4 to your computer and use it in GitHub Desktop.
git alias: local and remote branch deletion
[alias]
## Local branches ##
# deletion of merged branches through bang operator emulation
# a deletion is made only if, at least, a branch exists
clean-lbranches = "!f() { \
git branch --merged | \
grep -Ewv '^\\*|main|master|develop' | \
xargs -n1 -r git branch -d; \
}; f"
# force deletion
# /!\ beware, unmerged branches will be deleted /!\
force-clean-lbranches = "!f() { \
git branch --merged | \
grep -Ewv '^\\*|main|master|develop' | \
xargs -n1 -r git branch -D; \
}; f"
## Remote branches ##
# cleaning and syncing local cache
clean-rbranches = "!f() { \
git branch -r --merged | \
grep -Ewv 'main|master|develop' | \
sed 's/origin\\///' | \
xargs -n1 -r git push --delete origin && \
git fetch origin --prune; \
}; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment