Skip to content

Instantly share code, notes, and snippets.

@mikepenz
Last active October 13, 2016 19:25
Show Gist options
  • Save mikepenz/3186cbc81486e9753a159c74bde85ea5 to your computer and use it in GitHub Desktop.
Save mikepenz/3186cbc81486e9753a159c74bde85ea5 to your computer and use it in GitHub Desktop.
Syncs a defined branch from a forked GIT project with the external root directory

gitSync.sh

Why?

  • syncing with the main repo if we already have changes is a pain

Configure

  • define remoteOrigin (the external remote name of the root repo)
  • define ownOrigin (the remote name of our fork)
  • define syncBranch (the branch which we will get in sync)

What does it do?

  • remember the current branch
  • stash all current local changes
  • switch to the branch we want to sync
  • fetch all changes
  • pull all changes on the branch to get in sync from the original remote
  • push all changes to the forked remote
  • switch branch to the original one
  • reapply stashes
  • WIN! :D

How to use?

  • execute this shell script inside the git repo you want to get in sync
./gitSync.sh
#origin-main
remoteOrigin="the_main_repo_name"
#orign-fork
ownOrigin="our_fork_origin_name"
#develop
syncBranch="branch_we_want_to_get_in_sync"
currentBranch=$(git rev-parse --abbrev-ref HEAD)
echo "CURRENT BRANCH: ['${currentBranch}']"
stashOutput=$(git stash save "temporary_stash")
if [[ $stashOutput == *"No local changes"* ]]
then
addedToStash=false
else
addedToStash=true
fi
if [ "$addedToStash" = true ]
then
echo "STASHED YOUR CURRENT CHANGES";
else
echo "NO CHANGES ADDED TO STASH";
fi
echo "FETCH ALL CHANGES";
git fetch --all
echo "SWITCH TO BRANCH TO SYNC";
git checkout $syncBranch
echo "PULL THE LATEST REMOTE ORIGIN CHANGES";
git pull $remoteOrigin $syncBranch
echo "PUSH ALL CHANGES TO THE OWN ORIGN"
git push $ownOrigin $syncBranch
echo "CHECKOUT INITIAL BRANCH AGAIN"
git checkout $currentBranch
if [ "$addedToStash" = true ]
then
git stash pop > /dev/null
echo "REAPLIED YOUR STASHED CHANGES";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment