Skip to content

Instantly share code, notes, and snippets.

@p-karanthaker
Last active May 16, 2021 19:41
Show Gist options
  • Save p-karanthaker/5fdc78d78b3d0c6ec9b6655d3e7575b5 to your computer and use it in GitHub Desktop.
Save p-karanthaker/5fdc78d78b3d0c6ec9b6655d3e7575b5 to your computer and use it in GitHub Desktop.
Pulls the HEAD branch of all git repos in a directory and deletes branches which are no longer on remote
for gitDir in *; do
headBranch=$(git -C "$gitDir" symbolic-ref --short HEAD)
git -C "$gitDir" checkout "$headBranch"
git -C "$gitDir" pull
for branch in $(git -C "$gitDir" for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do
git branch -D $branch
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment