Skip to content

Instantly share code, notes, and snippets.

@cgmartin
Forked from EvanDotPro/gittyup.sh
Last active January 28, 2017 21:58
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 cgmartin/4046453 to your computer and use it in GitHub Desktop.
Save cgmartin/4046453 to your computer and use it in GitHub Desktop.
Easily keep master in sync with upstream.
#!/bin/bash
####################################################################################
## ##
## gittyup() - Easily keep master in sync with upstream. ##
## ##
## Author: Evan Coury, http://blog.evan.pro/ ##
## URL: https://gist.github.com/1506822 ##
## Forked: https://gist.github.com/cgmartin/4046453 ##
## ##
## 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(). ##
## ##
## By default gittyup() will sync with the upstream master. To sync with other ##
## upstream branches (like a git-flow 'develop' branch), pass the branch name ##
## as an argument to gittyup(): ##
## ##
## % gittyup develop ##
## ##
####################################################################################
## USAGE
#
# # Fork "main/repo" in Github to your personal remote repo
# % git clone git://personal/repo.git #<- A local clone of your personal remote repo
# % cd repo
# % git remote add upstream https://github.com/main/repo.git #<- The main repo you forked from
#
# % git checkout -b newfeature #<-- Now in "newfeature" branch...
# % vi somefile.txt #<-- made some edits to a file, but haven't added or committed yet
#
# You suddenly hear there was an update in the upstream repo that is required for your work...
#
# % gittyup
# # local master now has the updates from upstream
#
# ...then commit and rebase in our newbranch
#
# % git commit -am "my changes"
# % git rebase master # integrate our changes on top of master
# % git push -f origin newfeature
#
function gittyup()
{
if [[ -z "$1" ]]; then
branch="master"
else
branch=$1
fi
local gitCheck=$(git log -n1 2>&1 1>/dev/null | wc -l)
if [[ ! "$gitCheck" -eq "0" ]]; then
echo "Error: You are 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" != "$branch" ]]; then
git checkout $branch
fi
git fetch --all
git merge upstream/$branch --ff-only
git push origin $branch
if [[ "$currentBranch" != "$branch" ]]; then
git checkout $currentBranch
fi
if [[ ! "$stashed" -eq "0" ]]; then git stash pop --index; fi
}
gittyup $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment