Skip to content

Instantly share code, notes, and snippets.

@rpavlovic
Forked from ianmariano/git-fork-sync
Last active August 29, 2015 14:15
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 rpavlovic/60fdfd712ae119949432 to your computer and use it in GitHub Desktop.
Save rpavlovic/60fdfd712ae119949432 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage() {
echo "$0 usage:"
echo ""
echo " $0 [options]"
echo ""
echo "Where [options] include:"
echo ""
echo " -h|--help Show this help"
echo " -r|--remote REMOTE_NAME Fetch only this remote"
echo " -t|--topic TOPIC_BRANCH And rebase master onto this"
echo " topic branch after syncing"
echo " -u|--upstream TOPIC_BRANCH And push synced master here too"
echo ""
echo "If remote is not specified, all remotes are fetched"
echo "and your local master is rebased with upstream/master"
echo "then pushed to your origin."
echo ""
echo "If remote is specified, only that remote is fetched"
echo "and your local master is rebased to remote/master"
echo "then pushed to your origin."
echo ""
echo "-t|--topic may be used multiple times"
echo "-u|--upstream may be used multiple times"
exit 1
}
REMOTE=""
TOPICS=()
UPSTREAMS=()
while [ $# -ge 1 ]; do
key="$1"
shift
case $key in
-h|--help)
usage
exit 1
;;
-r|--remote)
REMOTE="$1"
shift
;;
-t|--topic)
TOPICS+=("$1")
shift
;;
-u|--upstream)
UPSTREAMS+=("$1")
shift
;;
*)
usage
exit 1
;;
esac
done
if ! git diff --quiet; then
echo "You have uncommitted changes."
exit 1
fi
set -e
git checkout master
if [ -z "$REMOTE" ]; then
git pull --rebase --all
git rebase upstream/master
git push origin master
echo "Now pruning all remotes"
all=`git remote`
for r in $all; do
echo -n "$r... "
git remote prune $r && echo "done."
done
else
echo "Only syncing $REMOTE"
git pull --rebase
git fetch $REMOTE
git rebase $REMOTE/master
git push origin master
git remote prune $REMOTE
fi
if [ ${#TOPICS[@]} -gt 0 ]; then
echo "Rebasing topic branches..."
for t in ${TOPICS[@]}
do
echo "...onto $t"
git rebase master $t
done
git checkout master
fi
if [ ${#UPSTREAMS[@]} -gt 0 ]; then
echo "Pushing synced master..."
for u in ${UPSTREAMS[@]}
do
echo "...to $u"
git push $u master
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment