Skip to content

Instantly share code, notes, and snippets.

@clockworksoul
Last active November 24, 2021 17:06
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 clockworksoul/1029ce118194d8662d4e8b7d21652f00 to your computer and use it in GitHub Desktop.
Save clockworksoul/1029ce118194d8662d4e8b7d21652f00 to your computer and use it in GitHub Desktop.
Syncs a fork with its parent repo (so you don't have to)
#!/usr/bin/env bash
# For best results, drop this into your path as "git-sync" and "chmod +x" it.
#
# From that point forward, you'll be able to use "git sync" as a command to
# synchronize any repository with a registered upstream.
set -eo pipefail
CURRENT_BRANCH=$(git status | grep '^On branch ' | sed 's/On branch //')
if [[ -n "$1" ]]; then
TARGET_BRANCH="$1"
else
TARGET_BRANCH=$CURRENT_BRANCH
echo "No branch specified. Using current ($CURRENT_BRANCH)"
fi
if [[ ! $(git remote -v | grep 'upstream') ]]; then
echo "No configured upstream repository. Use: git remote add upstream git@github.com:ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git"
exit 1
fi
if [[ "$CURRENT_BRANCH" != "$TARGET_BRANCH" ]]; then
git checkout "$TARGET_BRANCH"
fi
git fetch upstream
git merge "upstream/$TARGET_BRANCH"
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment