Skip to content

Instantly share code, notes, and snippets.

@Ovid
Created August 4, 2017 13:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ovid/e7ddc56c4710c1baae228b7e4dd3cf77 to your computer and use it in GitHub Desktop.
Save Ovid/e7ddc56c4710c1baae228b7e4dd3cf77 to your computer and use it in GitHub Desktop.
git-refresh
#!/bin/bash
# vim: filetype=sh
set -e # exit if any command fails
prog=$(basename $0)
branch=$(git rev-parse --abbrev-ref HEAD)
need_to_stash=$(git status --porcelain -uno)
if [ "$branch" == "master" ]; then
git fetch -p
if git rev-parse '@{u}' >/dev/null 2>&1; then
if [[ $need_to_stash ]]; then
git stash save "stashed by $prog (on branch $branch)"
git fetch -p
git pull --ff-only
git stash pop --index
else
git pull --ff-only
fi
fi
exit 0
else
if [[ $need_to_stash ]]; then
git stash save "stashed by $prog (on branch $branch)"
fi
git checkout master
git fetch -p
git pull --ff-only
git checkout $branch
if [[ $(git rebase master) ]]; then
if [[ $need_to_stash ]]; then
git stash pop
fi
else
echo git rebase failed.
if [[ $need_to_stash ]]; then
echo You have changes stashed by $prog
exit 1
fi
fi
fi
# Regardless of the branch you are on, this code:
# - stashes changes (if any)
# - checks out master (if not on master)
# - Do a fast-forward merge
# - checks out your branch (if it's not master)
# - rebases onto master (if branch is not master)
# - pops changes from stash, if any
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment