Skip to content

Instantly share code, notes, and snippets.

@MacDada
Created March 30, 2023 14:52
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 MacDada/63bab16eedb36292fb769d9a2561d812 to your computer and use it in GitHub Desktop.
Save MacDada/63bab16eedb36292fb769d9a2561d812 to your computer and use it in GitHub Desktop.
Synchronizes current branch with remote's master
#!/bin/bash
##
# Synchronizes current branch with remote's master.
#
# `./git_sync.sh` – updates current branch with changes from remote/master
# `./git_sync.sh push` – updates remote/master with changes from current branch
##
count_stashes () {
# `wc` generates a value with whitespace
# `bc` converts it to a number
git stash list | wc -l | bc
}
(echo ''; set -x; git status)
original_stashes_count=$(count_stashes)
(echo ''; set -x; git stash)
stashes_count_after_stash=$(count_stashes)
original_branch=$(git branch --show-current)
(echo ''; set -x; git checkout master)
(echo ''; set -x; git pull -r)
(echo ''; set -x; git checkout "${original_branch}")
(echo ''; set -x; git rebase master)
if [ "push" == "${1}" ]; then
(echo ''; set -x; git checkout master)
(echo ''; set -x; git merge "${original_branch}")
(echo ''; set -x; git push origin master)
(echo ''; set -x; git checkout "${original_branch}")
(echo ''; set -x; git rebase master)
fi
if [ "$original_stashes_count" != "$stashes_count_after_stash" ]; then
(echo ''; set -x; git stash pop)
else
(echo ''; set -x; git status)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment