feat: add git branch clean up function

This commit is contained in:
DannyDannyDanny 2026-02-25 12:57:20 +01:00
parent 19132a127d
commit d3e1fbb85d
3 changed files with 25 additions and 0 deletions

View file

@ -5,6 +5,7 @@
enable = true;
shellAliases = {
cdtmp = "cd $(mktemp -d)";
gclean = "git cleanup-branches";
theme = "bash ~/dotfiles/scripts/theme.sh";
music = "mpv --no-video --log-file=~/music_history.log \"$(find $HOME/Music/ -type f \\( -name '*.mp3' -o -name '*.wav' -o -name '*.flac' -o -name '*.m4a' -o -name '*.ogg' \\) | fzf)\"";
weather = "curl wttr.in/?T";

View file

@ -74,6 +74,9 @@
core = {
editor = "nvim";
};
alias = {
"cleanup-branches" = "!bash ~/dotfiles/scripts/git-cleanup-branches.sh";
};
};
};

21
scripts/git-cleanup-branches.sh Executable file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -e
git rev-parse --git-dir >/dev/null 2>&1 || { echo "Not a git repository." >&2; exit 1; }
default_branch="main"
git rev-parse --verify refs/heads/main &>/dev/null || default_branch="master"
git branch --merged "$default_branch" \
| grep -Fv "$default_branch" \
| grep -vF '*' \
| grep -vF '+' \
| xargs git branch -d \
&& git fetch \
&& git remote prune origin \
&& git branch -v \
| grep -F '[gone]' \
| grep -vF '*' \
| grep -vF '+' \
| awk '{print $1}' \
| xargs git branch -D