Skip to content

Instantly share code, notes, and snippets.

@patrick-yi-82
Forked from fernandoaleman/INSTALL.txt
Last active March 19, 2020 10:30
Show Gist options
  • Save patrick-yi-82/6b49fd5491e60ee06860f077d6dcc341 to your computer and use it in GitHub Desktop.
Save patrick-yi-82/6b49fd5491e60ee06860f077d6dcc341 to your computer and use it in GitHub Desktop.
Shell script to sync remote branches from upstream and push them up to forked origin
#!/bin/sh
UPSTREAM=$1
MYREPO=$2
usage() {
echo "Usage:"
echo "$0 <upstream-remote> <target-remote>"
echo ""
echo "Example which ensures remote named 'origin' have all the same branches and tags as 'upstream'"
echo "$0 upstream origin"
exit 1
}
if [ -z "$UPSTREAM" ]
then
echo "Missing upstream remote name."
usage
fi
if [ -z "$MYREPO" ]
then
echo "Missing target remote name."
usage
fi
read -p "1. This will setup '$MYREPO' to track all branches in '$UPSTREAM' - Are you sure ?" -n 1 -r
git clone $MYREPO myrepo
cd myrepo
git remote add upstream $UPSTREAM
git fetch upstream
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "\n"
for brname in `git branch -r | grep "upstream" | grep -v master | grep -v HEAD | sed -e 's/.*\///g'`; do git branch --track $brname upstream/$brname ; done
fi
read -p "2. This will push all local branches and tags into '$MYREPO' - Are you sure ?" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "\n"
git push --all
git push --tags
fi
1. Copy 'git-sync-fork' script code from gist
2. Create a file called 'git-sync-fork' in any 'bin' directory in your $PATH
3. Paste script into this new file 'git-sync-fork' and save
4. Make the file executable `chmod +x git-sync-fork`
5. Run the script inside your locally forked git repo
Example:
````
wget -O git-sync-fork.sh https://gist.githubusercontent.com/patrick-yi-82/6b49fd5491e60ee06860f077d6dcc341/raw/1c880a88eb6ae86d2fa2db4af9334039f1cc4233/git-sync-fork
chmod +x git-sync-fork.sh
./git-sync-fork.sh https://github.com/some/upstream-repo https://github.com/my/repo
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment