-
-
Save racecarparts/7f0af92224864cb4d752ce124785ab3e 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 hidden or 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 | |
gh_sync_master() { | |
UPSTREAM='upstream' | |
MYREPO='origin' | |
MASTER_BRANCH='master' | |
AUTOMAGIC=true | |
sync_upstream $UPSTREAM $MYREPO $MASTER_BRANCH $AUTOMAGIC | |
} | |
gh_sync_develop() { | |
UPSTREAM='upstream' | |
MYREPO='origin' | |
MASTER_BRANCH='develop' | |
AUTOMAGIC=true | |
sync_upstream $UPSTREAM $MYREPO $MASTER_BRANCH $AUTOMAGIC | |
} | |
gh_sync_development() { | |
UPSTREAM='upstream' | |
MYREPO='origin' | |
MASTER_BRANCH='development' | |
AUTOMAGIC=true | |
sync_upstream $UPSTREAM $MYREPO $MASTER_BRANCH $AUTOMAGIC | |
} | |
sync_upstream() { | |
UPSTREAM=$1 | |
MYREPO=$2 | |
MASTER_BRANCH=$3 | |
AUTOMAGIC=$4 | |
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD` | |
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" | |
kill -INT $$ | |
} | |
if [ -z "$UPSTREAM" ] | |
then | |
echo "Missing upstream remote name." | |
usage | |
fi | |
if [ -z "$MYREPO" ] | |
then | |
echo "Missing target remote name." | |
usage | |
fi | |
if [ "$AUTOMAGIC" = true ] | |
then | |
REPLY='y' | |
else | |
read -p "1. This will setup '$MYREPO' to track all branches in '$UPSTREAM' - Are you sure ?" -n 1 -r | |
echo -e "\n" | |
fi | |
echo "Fetching '$UPSTREAM'" | |
git fetch $UPSTREAM | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
for brname in `git branch -r | grep "$UPSTREAM" | grep -v $MASTER_BRANCH | grep -v HEAD | sed -e 's/.*\///g'`; do git branch --track $brname $UPSTREAM/$brname ; done | |
fi | |
if [ "$AUTOMAGIC" = true ] | |
then | |
REPLY='y' | |
else | |
read -p "1. This will merge '$UPSTREAM/$MASTER_BRANCH' into '$MASTER_BRANCH' - Are you sure ?" -n 1 -r | |
echo -e "\n" | |
fi | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "Merging $UPSTREAM/$MASTER_BRANCH into local $MASTER_BRANCH..." | |
git checkout $MASTER_BRANCH | |
git merge $UPSTREAM/$MASTER_BRANCH | |
git checkout $CURRENT_BRANCH | |
fi | |
if [ "$AUTOMAGIC" = true ] | |
then | |
REPLY='y' | |
else | |
read -p "1. This will remove any local branches not on '$MYREPO' - Are you sure ?" -n 1 -r | |
echo -e "\n" | |
fi | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "Pruning local branches not found in '$MYREPO'..." | |
git remote prune $MYREPO | |
fi | |
if [ "$AUTOMAGIC" = true ] | |
then | |
REPLY='y' | |
else | |
read -p "2. This will push all local branches and tags into '$MYREPO' - Are you sure ?" -n 1 -r | |
echo -e "\n" | |
fi | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "Pushing branches..." | |
git push --all $MYREPO | |
echo "Pushing tags..." | |
git push --tags $MYREPO | |
fi | |
} |
This file contains hidden or 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.sh' in any directory available to you | |
3. Paste script into this new file 'git-sync-fork.sh' and save | |
4. Source the file (in your bash profile script) `source /path/to/file/git-sync-fork.sh` | |
5. Call either function: `gh_sync_[master|develop|development]` (for auto confirmation) or `sync_upstream upstream origin` | |
Example: | |
gh_sync_master | |
gh_sync_develop | |
gh_sync_development | |
OR | |
sync_upstream upstream origin master true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment