Skip to content

Instantly share code, notes, and snippets.

@iamahuman
Last active April 4, 2021 13:08
Show Gist options
  • Save iamahuman/5a8e510fbcedfa31c0acb03961a320fb to your computer and use it in GitHub Desktop.
Save iamahuman/5a8e510fbcedfa31c0acb03961a320fb to your computer and use it in GitHub Desktop.
[[Archived bad quality mess when I used to not know better - just use git fetch && git push upstream 'refs/remotes/A/*:refs/heads/*' instead of this crap!]] Synchronize origin's branches with upstream counterparts (NOTE: assumes development model of keeping track of local changes with separate branch names)
#!/bin/bash
set -e
old_pwd="$PWD"
do_msg() {
printf "\\r[%s:%s] - %s" "$reponame" "$branch" "$*" >&2
}
do_end_msg() {
printf "\\r[%s:%s] - %s" "$reponame" "$branch" "$*" >&2
}
for repo in "$@"; do :
if [ -r "$repo/.git/HEAD" ] && cd "$repo"; then :
reponame=$(basename "$repo")
if git diff-index --quiet --cached HEAD --
then if git diff-files --quiet
then :
old_branch="$(git rev-parse --abbrev-ref HEAD)"
if git config remote.upstream.url > /dev/null
then upstream=y
else upstream=n
fi
git for-each-ref refs/heads --format='%(refname)' | cut -d/ -f3- | while IFS=$'\n' read -r branch
do :
if ! git checkout -q "$branch"; then :
do_end_msg "checkout failed!"
continue
fi
do_msg "pulling from remote origin"
if ! git pull origin "$branch"; then :
do_end_msg "pulling from remote origin..FAILED!" >&2
continue
fi
if [ x$upstream = xy ]; then :
do_msg "pulling from remote upstream.."
if ! git pull upstream "$branch"; then :
do_end_msg "pulling from remote upstream..FAILED!"
continue
fi
do_msg "pushing to remote origin.."
if ! git push origin "$branch"; then :
do_end_msg "pushing to remote upstream..FAILED!"
continue
fi
fi
do_end_msg "DONE!"
done
git checkout -q "$old_branch"
else echo "Repository $reponame has unstaged changes, aborting." >&2; exit 16
fi
else echo "Repository $reponame has staged changes that has yet not been commited, aborting." >&2; exit 17
fi
fi
cd "$old_pwd"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment