Skip to content

Instantly share code, notes, and snippets.

@apzentral
Last active May 22, 2024 17:14
Show Gist options
  • Save apzentral/a9c67e4b4c2fb6505c25a684e2e5161a to your computer and use it in GitHub Desktop.
Save apzentral/a9c67e4b4c2fb6505c25a684e2e5161a to your computer and use it in GitHub Desktop.
git script to remove all local branches that not exists in the remote
#!/bin/bash
# Fetch the latest changes from the remote and prune deleted branches
git fetch --prune
# Get a list of all local branches
local_branches=$(git branch --format='%(refname:short)')
# Get a list of all remote branches
remote_branches=$(git branch -r --format='%(refname:short)' | sed 's/origin\///')
# Loop through each local branch
for branch in $local_branches; do
# Check if the local branch exists on the remote
if ! echo "$remote_branches" | grep -q "^$branch$"; then
# If the branch does not exist on the remote, delete it
echo "Deleting local branch: $branch"
git branch -d "$branch"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment