Skip to content

Instantly share code, notes, and snippets.

@EvanDotPro
Created December 21, 2011 17:15
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save EvanDotPro/1506822 to your computer and use it in GitHub Desktop.
Save EvanDotPro/1506822 to your computer and use it in GitHub Desktop.
Easily keep master in sync with upstream.
####################################################################################
## ##
## gittyup() - Easily keep master in sync with upstream. ##
## ##
## Author: Evan Coury, http://blog.evan.pro/ ##
## URL: https://gist.github.com/1506822 ##
## ##
## This bash function is a simple shortcut for keeping your local (and public ##
## fork / origin remote) master branch up to date and in sync with the upstream ##
## master. To use gittyup(), simply drop this in your ~/.bashrc. ##
## ##
## A few assumptions are made: ##
## ##
## - The remote you are updating from is named upstream. ##
## - You have a remote named origin that you can push to. ##
## - You always work in topic branches and your local master branch has not ##
## diverged from the upstream master. ##
## ##
## gittyup() performs all of the following tasks for you: ##
## ##
## 0. Verify that you are in a valid Git repo. ##
## 1. Remember which branch you are on. ##
## 2. Stash any uncommitted changes you have. ##
## 3. Checkout master. ##
## 4. Fetch all remotes. (nice to track other remotes) ##
## 5. Merge upstream/master into local master (FF ONLY!) ##
## 6. Push your updated local master branch to origin. ##
## 7. Check out the branch you were previously on. ##
## 8. Applies any stashed changes and index status. ##
## ##
## It _should_ be safe to run gittyup() in any repo where the assumptions above ##
## hold true. You can run gittyup() even when your repository is in a dirty ##
## state (uncommitted / unstaged changes) and regardless of which branch you ##
## are working in. After gittyup() is finished, it will return you to the ##
## branch you were working in and restore your working tree and index to the ##
## exact state it was in before running gittyup(). ##
## ##
####################################################################################
function gittyup()
{
local gitCheck=$(git log -n1 2>&1 1>/dev/null | wc -l)
if [[ ! $gitCheck -eq 0 ]]; then
echo "Error: You're not in a Git repository"
return
fi
local currentBranch=$(git branch | grep \* | cut -d' ' -f2)
local stashed=$(git stash | grep -v 'No local changes' | wc -l)
if [[ ! $stashed -eq 0 ]]; then
echo "Your working copy has uncommitted changes..."
echo -e "These changes have been stashed and will be re-applied when we're done.\n"
fi
if [[ $currentBranch != 'master' ]]; then
git checkout master
fi
git fetch --all
git merge upstream/master --ff-only
git push origin master
if [[ $currentBranch != 'master' ]]; then
git checkout $currentBranch
fi
if [[ ! $stashed -eq 0 ]]; then git stash pop --index; fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment