Last active
August 29, 2015 14:10
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'.