Skip to content

Instantly share code, notes, and snippets.

@orirawlings
Last active April 12, 2021 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orirawlings/5df68dfa7d650d093764a80f0c39e6c0 to your computer and use it in GitHub Desktop.
Save orirawlings/5df68dfa7d650d093764a80f0c39e6c0 to your computer and use it in GitHub Desktop.
git-grounded: A small script to cleanup a local git repository after feature development
#! /bin/bash
set -e
remote=${1:-origin}
# Determine local HEAD
head=$(git symbolic-ref HEAD)
# Determine main branch of remote repo
remote_head=$(git ls-remote --symref ${remote} HEAD | awk '$1=="ref:" && $3=="HEAD" {print $2}')
# Bail if local HEAD doesn't match the name of main branch on remote
if [[ $head != $remote_head ]]
then
printf "Error: HEAD of remote %s is %s which is different than local HEAD, %s. Please update local HEAD to match and try again.\n" $remote $remote_head $head
exit 1
fi
# Pull changes from remotes
git pull --ff-only
# Prune any remote branches that no longer exist in upstream remotes
git remote prune $(git remote)
# Prune any local branches that have already been merged into the main branch
git for-each-ref --merged $head | awk -v head=$head '/refs\/heads\// && match($3, head)==0 {printf("delete %s %s\n", $3, $1)}' | git update-ref --stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment