#!/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 master 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 master # Rebase the current single commit onto the latest. git rebase master # 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.

This script will assist you in ensuring that your pull requests are squashed and rebased on the latest master.


To run, execute the following command in your terminal:

curl rebaseandsqua.sh | sh

The script will squash your current work into a single commit, prompt you for a commit message, and then attempt to rebase on the latest changes from master.

Under The Hood

How does the script work?

You can view a full copy of the commands it runs to the right, or simply view the source of this page!

What About git rebase -i?

Learn it and use it! This script is for newcomers to rebasing and squashing who are contributing to open source projects.