Skip to content

Instantly share code, notes, and snippets.

@abrambailey
Forked from DennisOSRM/rebase.sh
Last active June 30, 2016 18:54
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 abrambailey/2d17199bf6ea3dfe076ad454b0e1c4e9 to your computer and use it in GitHub Desktop.
Save abrambailey/2d17199bf6ea3dfe076ad454b0e1c4e9 to your computer and use it in GitHub Desktop.
Rebase current branch against origin/qa and squash all commits into a single one
#!/bin/sh
set -e
# Get the current branch name.
branch=`git rev-parse --abbrev-ref HEAD`
# Determine the commit at which the current branch diverged.
ancestor=`git merge-base qa HEAD`
# Stash any uncommited, changed files.
git stash --all
# Revert the branch back to the ancestor SHA.
git reset --hard $ancestor
# Squash all commits from ancestor to previous SHA.
git merge --squash HEAD@{1}
# Perform the commit, prompting for the message.
git commit
# Fetch the latest changes from the upstream branch.
git fetch origin qa
# Rebase the current single commit onto the latest.
git rebase qa
# Restore previous uncommited changes, if any.
git stash pop || true
printf "\nDone! Push with 'git push -f origin %s'\n" "$branch"
exit 0 # High five! Everything worked.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment