Skip to content

Instantly share code, notes, and snippets.

@OleksandrKucherenko
Created June 30, 2016 07:31
Show Gist options
  • Save OleksandrKucherenko/0999d809a05a9589536d7ac3c47b4102 to your computer and use it in GitHub Desktop.
Save OleksandrKucherenko/0999d809a05a9589536d7ac3c47b4102 to your computer and use it in GitHub Desktop.
Get Latest version of the source code from Upstream Master and rebase own branch against it.
#!/bin/bash
# wait for Enter key
function pause(){
read -p "$*"
}
MASTER=master
BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)"
CHANGED_FILES="$(git status --porcelain --untracked-files=no | wc -l)"
echo ''
echo 'This script will REBASE current branch against "master". Current branch is:' $BRANCH_NAME
echo 'If its "master" already than we just update the branch to latest'
echo 'Detected changed file(s):' $CHANGED_FILES
echo ''
if [ $CHANGED_FILES -gt 0 ]; then
pause 'Press [Enter] to stash the changes'
# save current changes to STASH
git stash
fi
echo ''
pause 'Press [Enter] to start the pull...'
# don't checkout master if we already in it
if [ "$BRANCH_NAME" != "$MASTER" ]; then
# switch git to MASTER branch
git checkout master
fi
# get updates of the MASTER
git pull --rebase upstream master
# update submodules and reset them to initial state
git pull --recurse-submodules
git submodule update --init --recursive
# if branch NOT a master than checkout it
if [ "$BRANCH_NAME" != "$MASTER" ]; then
# return back to our branch
git checkout $BRANCH_NAME
else
echo "We are the MASTER's of the world..."
fi
echo ''
pause 'Press [Enter] to start merge...'
# Can be used MERGE operation instead of REBASE
#git merge master
# let's reduce the complexity of the PULL REQUEST's by using the REBASE
git rebase master
# http://www.gitguys.com/topics/merging-with-a-gui/
echo ''
echo 'In case of conflicts resolving needs, use:'
echo ':: git mergetool -t kdiff3'
echo ''
# if we have changes before we start, than recover them
if [ $CHANGED_FILES -gt 0 ]; then
pause 'Press [Enter] to apply stash...'
# apply changes and remove stash
git stash pop
else
echo "No changes in STASH that we have to apply. Good work, keep it clean!"
fi
if whiptail --yesno "Run submodules fix?" 10 30 ;then
./git-fix-submodules.sh
else
echo "We are lucky, no submodules issues."
fi
if whiptail --yesno "Run the cleanup?" 10 30 ;then
./gradlew clean
./gradlew --stop
rm -rf .gradle
else
echo "No cleanup, maybe next time we will do that."
fi
echo ''
echo All Done. Goodbye.
exit 0