Skip to content

Instantly share code, notes, and snippets.

@larruda
Last active August 29, 2015 14:03
Show Gist options
  • Save larruda/1c813d8f459a947b673c to your computer and use it in GitHub Desktop.
Save larruda/1c813d8f459a947b673c to your computer and use it in GitHub Desktop.
Syncs a forked repository against its upstream.
#!/bin/sh
git_repo=$1
[ -z "$git_repo" ] && git_repo="."
cd $git_repo > /dev/null 2>&1
if [ $? -ne 0 ]; then
printf "Invalid directory path.\n"
exit 1
fi
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
col=$(tput cols)
git status > /dev/null 2>&1
git fetch upstream > /dev/null 2>&1
if [ $? -ne 0 ]; then
printf "Not a git repository or no upstream found for %s.\n" "$git_repo"
exit 1
fi
upstream_branches=`git branch -r | grep upstream | cut -c3-`
for branch_name in $upstream_branches
do
printf "Syncing branch %s of %s... " "$branch_name" "$git_repo"
local_name=`echo $branch_name | cut -c10-`
git checkout -b $local_name $branch_name > /dev/null 2>&1
git checkout $local_name
git merge $branch_name
git push origin $local_name
if [ $? -eq 0 ]; then
printf '%s%*s%s' "$GREEN" $col "[OK]" "$NORMAL"
else
printf '%s%*s%s' "$RED" $col "[FAIL]" "$NORMAL"
fi
done
@larruda
Copy link
Author

larruda commented Jul 29, 2014

Usage: ./forksync.sh [repository_path]

@larruda
Copy link
Author

larruda commented Jul 29, 2014

Put it on your crontab and be happy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment