Last active
April 14, 2021 09:54
-
-
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
This file contains 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/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 |
This file contains 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
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 |
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
Adding
git fetch $UPSTREAM
would be helpful