Skip to content

Instantly share code, notes, and snippets.

@vitr
Forked from EvanDotPro/gittyup.sh
Created May 25, 2013 09:38
Show Gist options
  • Save vitr/5648521 to your computer and use it in GitHub Desktop.
Save vitr/5648521 to your computer and use it in GitHub Desktop.
###############################################################
## ##
## gittyup() - Easily keep master in sync with upstream. ##
## ##
## Author: Evan Coury, http://blog.evan.pro/ ##
## URL: https://gist.github.com/{TBD} ##
## ##
## 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. ##
## - Your local master branch has not diverged from the ##
## upstream master (always work in topic branches) ##
## ##
## 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 != 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 != 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
git checkout master
git fetch --all
git merge upstream/master --ff-only
git push origin master
git checkout $currentBranch
if [[ $stashed != 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