Skip to content

Instantly share code, notes, and snippets.

@ricardobeat
Last active April 14, 2020 18:19
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardobeat/9600953 to your computer and use it in GitHub Desktop.
Save ricardobeat/9600953 to your computer and use it in GitHub Desktop.
git remaster

git-remaster updates both your current branch and master branch simultaneously, while preserving all commited, staged and unstaged changes.

Install

Add git-remaster to your path and make it executable:

curl https://gist.githubusercontent.com/ricardobeat/9600953/raw/git-remaster.sh > /usr/local/bin/git-remaster
chmod +x /usr/local/bin/git-remaster
#!/bin/bash
## git-remaster
# Pulls latest remote HEAD to your master and rebases current branch on top of it, preserving staged and unstaged changes.
set -e
if git show-ref --quiet --verify refs/heads/master; then MASTER=master; fi
if git show-ref --quiet --verify refs/heads/trunk; then MASTER=trunk; fi
if [ -z $MASTER ]; then echo "No master/trunk branch found."; exit 1; fi
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# save current state to a detached commit object and reset
RSTASH=$(git stash create --include-untracked)
git reset --hard
[[ $RSTASH ]] && echo "Stashed $RSTASH"
# fetch updates
git fetch origin
# if not on master branch, check it out, update and return to branch
if [[ $BRANCH != $MASTER ]]; then
git checkout $MASTER
git rebase origin/$MASTER --preserve-merges
git checkout -
fi
# rebase changes on top of origin/master
git rebase origin/$MASTER --preserve-merges
# bring back stash if available, hide errors when empty
test $RSTASH && git stash apply $RSTASH 2>/dev/null
@almirfilho
Copy link

Foda!

@devthiago
Copy link

Digamos que aprendi algo... Foda[2]!

@v42
Copy link

v42 commented Apr 23, 2014

one word: afudê!

@ricardobeat
Copy link
Author

Note: if you get merge conflicts, remember you are either in the middle of a rebase, or after stash apply.

@bpassos-zz
Copy link

Lindo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment