Skip to content

Instantly share code, notes, and snippets.

@fernandoaleman
Last active April 14, 2021 09:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save fernandoaleman/661b50b6f5bb42d9f4be3c9bc0401249 to your computer and use it in GitHub Desktop.
Save fernandoaleman/661b50b6f5bb42d9f4be3c9bc0401249 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
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 $MYREPO
git push --tags $MYREPO
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:
git-sync-fork upstream origin
@patrick-yi-82
Copy link

Adding git fetch $UPSTREAM would be helpful

@racecarparts
Copy link

racecarparts commented May 9, 2019

I wrapped this gist (git-sync-fork) in a function. The to prevent the shell from exiting ... when no params are given:

sync_upstream() {

...

  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
+   kill -INT $$
  }

...

}

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