Skip to content

Instantly share code, notes, and snippets.

@a7madgamal
Last active January 18, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a7madgamal/3f49bdda66bd437c7f25c4718088a42c to your computer and use it in GitHub Desktop.
Save a7madgamal/3f49bdda66bd437c7f25c4718088a42c to your computer and use it in GitHub Desktop.
stash changes > fetch all branches > pull all branches from remote > merge each local branch with master > pop stash > enjoy!
#!/usr/bin/env bash
I="\e[96m"
E="\e[90m"
echo -e "πŸ€–$I starting sync.. $E";
CURRENT_BRANCH="`git rev-parse --abbrev-ref HEAD`"
STASH_RESULT="`git stash push -u -m AUTO_STASH | grep Saved`"
if [ -n "$STASH_RESULT" ]; then
echo -e "πŸ€–$I stashed changes to AUTO_STASH $E";
fi
git fetch --all -p
LOCAL_BRANCHES=`git branch | cut -c 3-`;
BRANCHES_WITH_REMOTES=`git for-each-ref --format '%(refname:short):%(upstream:short)' 'refs/heads' | grep -v ':$'`;
echo $BRANCHES_WITH_REMOTES | while IFS=: read local remote; do
echo -e "πŸ€–$I running git checkout $local $E"
git checkout $local || return 1;
echo -e "πŸ€–$I running git pull $E"
git pull;
done
echo $LOCAL_BRANCHES | while IFS=: read local; do
echo -e "πŸ€–$I running git checkout $local $E"
git checkout $local || return 1;
echo -e "πŸ€–$I merging $local with master $E"
git merge master || return 1;
done
echo -e "πŸ€–$I checkingout starting branch $CURRENT_BRANCH $E"
git checkout $CURRENT_BRANCH
if [ -n "$STASH_RESULT" ]; then
git stash pop;
else
echo -e "πŸ€–$I no changes found. skipping stash pop $E";
fi
echo -e "πŸ€–$I All good πŸŽ‰πŸŽ‰ $E";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment