Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell script to synchronise git fork to an upstream repository and prune/delete local branches which are merged
#!/bin/sh
#
# This script helps to automatically synchronise a GitHub fork to the upstream repository
#
# It will perform the following functions:
# - Fetch from upstream
# - Prune branches (disconnect from remote)
# - Swap to the master branch
# - Merge upstream/master into origin/master
# - Remove local branches which have been pruned
#
# NOTE: The upstream repository should be added with:
# $ git remote add upstream <url/ssh>
#
# NOTE: Changes should be pushed to the origin repository after
#
# Mike Goodfellow 2015
# https://mikegoodfellow.co.uk
#
# Fetch upstream changes
git fetch upstream
# Prune deleted branches
git fetch -p
# Swap to master branch
git checkout master
# Merge in the changes from upstream/master
git merge upstream/master
# Check if any branches need pruning
git branch --merged master | grep -v 'master$'
if [ $? -eq 1 ]; then
echo "No local branches need deleting"
else
echo "Local branches need deleting"
git branch --merged master | grep -v 'master$' | xargs git branch -d
fi
# Update our fork master with any changes
git push
echo "Finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.