Last active
April 20, 2018 20:23
-
-
Save jakebathman/9c2927b82b065f47129ae7512913dfbd to your computer and use it in GitHub Desktop.
Update a repo fork with the original (upstream)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# sync a fork using the upstream branch | |
gfu(){ | |
BRANCH="$1" | |
if [ -z "$1" ]; then | |
BRANCH="master" | |
fi | |
echo -n "Fetch and merge upstream for branch $BWhite$BRANCH$NC? [y/N]? " | |
read answer | |
if echo "$answer" | grep -iq "^y" ;then | |
echo "" | |
else | |
return | |
fi | |
git remote -v | grep -qi "upstream" &> /dev/null | |
if [ $? != 0 ]; then | |
echo $BRed"upstream remote not set up"$NC | |
echo "adding upstream..." | |
UPSTREAM_URL="" | |
# Get the upstream repo URL | |
if [[ -a "package.json" ]]; then | |
UPSTREAM_URL=$(cat package.json | jq .repository.url | grep -Eo "(https?\:\/\/.+\.git)") | |
fi | |
echo "UPSTREAM_URL: $UPSTREAM_URL" | |
# If there's still no upstream, ask for it | |
if [ -z "$UPSTREAM_URL" ]; then | |
echo "Couldn't find the upstream URL :(\nPlease paste it here (e.g. https://github.com/org/repo.git): " | |
read upstream | |
if [ -z "$upstream" ]; then | |
echo "quitting..." | |
return | |
fi | |
UPSTREAM_URL="$upstream" | |
fi | |
echo $BPurple"git remote add upstream $UPSTREAM_URL"$NC | |
git remote add upstream "$UPSTREAM_URL" | |
git remote -v | grep "upstream" --color=none | |
fi | |
echo "Fetching upstream..." | |
echo $BCyan"git fetch upstream"$NC | |
git fetch upstream | |
echo "Branch: $BRANCH" | |
echo "\nChecking out local branch $BRANCH" | |
echo $BYellow"git checkout $BRANCH"$NC | |
git checkout "$BRANCH" | |
echo "\nMerging upstream into local" | |
echo $BGreen"git merge upstream/$BRANCH"$NC | |
git merge upstream/"$BRANCH" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment