Skip to content

Instantly share code, notes, and snippets.

@romgrk
Created July 14, 2016 07:27
Show Gist options
  • Select an option

  • Save romgrk/e8f8d7305d1f9f3c5d1ca778405b6322 to your computer and use it in GitHub Desktop.

Select an option

Save romgrk/e8f8d7305d1f9f3c5d1ca778405b6322 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# Usage: $ savepull [dir]
#
# Default:
# dir = ./
#
# Updates the master branch of the specified git repository
# from upstream (aka origin/master), but handles dirty directory
# by stashing the state, switching to master, then coming back to
# the initial branch & pop-stashing automatically.
local dir="$1"
if [[ -n $dir ]]; then
eval "cd $dir"
fi
local branch="$(git branch | noglob grep ^* | sed -r 's/\* //')"
local stashed='false'
if [[ !($branch == 'master') ]]; then
if [[ ! $(git status) =~ 'working directory clean$' ]]; then
stashed='true'
eval 'git stash'
fi
eval 'git checkout master'
fi
eval 'git pull'
if [[ !($branch == 'master') ]]; then
eval "git checkout $branch"
fi
if [[ $stashed == 'true' ]]; then
eval 'git stash pop'
fi
print Done.
# JSON License. 2016.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment