Skip to content

Instantly share code, notes, and snippets.

@liyu1981
Last active January 1, 2025 22:41
Show Gist options
  • Save liyu1981/98810ada84ed9ad1731b76db4fe9f4f0 to your computer and use it in GitHub Desktop.
Save liyu1981/98810ada84ed9ad1731b76db4fe9f4f0 to your computer and use it in GitHub Desktop.
git alias for prune remote branches & remove local branches not tracking any remote
# for zsh in macos, linux may need to change something
alias git-remove-list-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \$1}"'
# for squashed merged PRs
alias git-remove-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \
$1}" | xargs git branch -D'
# for non-squashed merged PRs
#alias git-remove-untracked='git fetch --prune && git branch -r | awk "{print \$1}" | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk "{print \
$1}" | xargs git branch -D'
alias git-remote-list-prune='git remote prune --dry-run origin'
alias git-remote-prune='git remote prune origin'
do_git_remove_untracked() {
# Array of commands and their descriptions
local commands=(
"git-remote-list-prune:List and prune remote branches"
"git-remote-prune:Prune stale remote branches"
"git-remove-list-untracked:List untracked files for removal"
"git-remove-untracked:Remove untracked files"
)
for cmd in "${commands[@]}"; do
local command="${cmd%%:*}" # Extract the command
local description="${cmd#*:}" # Extract the description
while true; do
echo -n "Do you want to run '${command}' (${description})? [y/n]: "
read yn
case $yn in
[Yy]* )
echo "Running: $command"
eval $command
break
;;
[Nn]* )
echo "Skipped: $command"
break
;;
* )
echo "Please answer y or n."
;;
esac
done
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment