Sync forked git repository

Here is a script to sync a forked (not mirrored) git repository. In my case I have new private branches I cannot push upstream, but I want the base branches to get synchronized with the upstream repository.

#!/bin/bash set -e # set -x UPSTREAM=$1 REMOTES=$(git remote -v) if [[ "$REMOTES" != *"$UPSTREAM"* ]]; then git remote add upstream $UPSTREAM fi git fetch git fetch upstream BRANCHES=$(git branch -a) REMOTES=(${BRANCHES// /}) ORIGIN=() UPSTREAM=() LOCAL=() for element in "${REMOTES[@]}" do if [[ "$element" == *"origin"* ]]; then ORIGIN+=(${element#"remotes/origin/"}) elif [[ "$element" == *"upstream"* ]]; then UPSTREAM+=(${element#"remotes/upstream/"}) else LOCAL+=(${element#"*"}) fi done for remote in "${UPSTREAM[@]}" do echo ${remote} echo "===========================================" if [[ " ${ORIGIN[@]} " =~ "${remote} " ]]; then echo "merging ${remote}:" if [[ " ${LOCAL[@]} " =~ " ${remote} " ]]; then git checkout ${remote} else git checkout -b ${remote} origin/${remote} fi git merge upstream/${remote} git push else echo "adding new branch ${remote}:" git checkout ${remote} git push --set-upstream origin ${remote} fi echo "" done
Code language: Bash (bash)

Usage – copy the script in your repository folder then ssh://upstream/remote/path
Code language: Bash (bash)

Leave a Reply

Your email address will not be published. Required fields are marked *