Skip to content

Instantly share code, notes, and snippets.

@ccll
Last active August 29, 2015 14:10
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 ccll/6486fd5e712e8e2b0b5b to your computer and use it in GitHub Desktop.
Save ccll/6486fd5e712e8e2b0b5b to your computer and use it in GitHub Desktop.
[git] Squash current feature branch in-place (from the last fork point to the tip of current branch)
#!/bin/bash
base=$1
if [ "$base" == "" ]; then
echo "Please specify a base branch"
echo "Usage: git squash <base branch>"
echo ""
echo "<base branch>: the branch where your current branch forked off (normally it's 'master')"
exit 1
fi
# Get the tip of current branch.
tip=$(git rev-parse HEAD)
echo "Current tip: $tip"
# Get the best common ancestor of current branch and $base.
from=$(git merge-base $tip $base)
echo "Squash from: $from"
# Squash from $point to the tip of current branch.
git reset --soft $from
git commit
# Ensure there are no differences between the original tip and the squashed one.
new=$(git rev-parse HEAD)
echo "-----------------------------------------------"
echo "Making sure the squash is correct by comparing:"
echo " original tip: $tip"
echo " squashed tip: $new"
git diff --exit-code $new $tip
ret=$?
if [ $ret -eq 0 ]; then
echo "Success!"
else
echo "[ERROR] There are differences after the squash, rolling back..."
git reset --hard ORIG_HEAD
echo "Your squash has been rolled back."
fi
exit $ret
@ccll
Copy link
Author

ccll commented Nov 20, 2014

git-extras provided an 'git-squash' implementation, but it is using 'git merge --squash' which I don't like very much, I'd prefer a in-place solution with 'git reset --soft'.

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