Created
July 14, 2016 07:27
-
-
Save romgrk/e8f8d7305d1f9f3c5d1ca778405b6322 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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