Skip to content

Instantly share code, notes, and snippets.

@derberg
Last active February 29, 2024 10:28
Show Gist options
  • Save derberg/87319e9c486e4a6c9bef5b629ab0d386 to your computer and use it in GitHub Desktop.
Save derberg/87319e9c486e4a6c9bef5b629ab0d386 to your computer and use it in GitHub Desktop.
This script synchronizes master branch of your fork with the master branch of upstream git repository
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
ORG_NAME=$1
REPO_NAME=$2
BRANCH=$3
function setRemoteUpstream() {
ORG_NAME=$1
REPO_NAME=$2
git remote add upstream https://github.com/$ORG_NAME/$REPO_NAME.git
}
if setRemoteUpstream $ORG_NAME $REPO_NAME
then
printf -- "\033[37m New upstream pointing to https://github.com/${ORG_NAME}/${REPO_NAME}.git is set \033[0m\n"
else
printf -- "\033[37m Making sure proper upstream is set to https://github.com/${ORG_NAME}/${REPO_NAME}.git \033[0m\n"
git remote remove upstream
setRemoteUpstream $ORG_NAME $REPO_NAME
fi
git fetch upstream $BRANCH
git branch -u upstream/$BRANCH $BRANCH
printf -- "\033[32m Now $BRANCH branch on you local fork is in sync with upstream $BRANCH branch \033[0m\n"
git branch -vv | grep $BRANCH

It is a script that I use to setup my forks. You perform such a setup only once. This means you do not do it very often. I'm not an elephant, I don't remember commands I use rarely. I like to have these things in scripts.

Use the script by passing two variables: bash conf_fork.sh <ORG_NAME> <REPO_NAME> <MASTER_OR_MAIN>

For example. If you want to contribute to generator to AsyncAPI and you forked the repo, then run the following in your terminal in the root of the repository:

bash conf_fork.sh asyncapi generator master

You can also add an alias to your bash profile to make your life easier:

alias conf-fork="bash ~/<SCRIPT_LOCATION/conf_fork.sh"

Feel free to suggest improvements. I know it is not perfect. Just please be polite 🍺

@fmvilas
Copy link

fmvilas commented Jul 11, 2023

Every time I have to setup my machine, I'm coming back to this. Thanks, mate! 🍺

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