Skip to content

Instantly share code, notes, and snippets.

@dsoprea
Created May 4, 2018 01:59
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 dsoprea/0d0e6d4cfab8608ca8c67dd47984caac to your computer and use it in GitHub Desktop.
Save dsoprea/0d0e6d4cfab8608ca8c67dd47984caac to your computer and use it in GitHub Desktop.
Script to squash the last Git commit
#!/bin/bash -e
HEAD_COMMIT_MESSAGE=$(git log --format=%B -1 HEAD)
# For safety. Our use-case is usually to always just squash into a commit
# that's associated with an active change. We really don't want lose our head
# and accidentally squash something that wasn't intended to be squashed.
if [[ "${HEAD_COMMIT_MESSAGE}" != SQUASH* ]]; then
echo "SQUASH: Commit to be squashed should have 'SQUASH' as its commit-message."
exit 1
fi
_FILEPATH=$(mktemp)
git log --format=%B -1 HEAD~1 >"${_FILEPATH}"
echo "Initial head: $(git rev-parse HEAD)"
git reset --soft HEAD~2 >/dev/null
echo "Head after reset: $(git rev-parse HEAD)"
git commit -F $_FILEPATH >/dev/null
rm $_FILEPATH
echo "Head after commit: $(git rev-parse HEAD)"
echo
# Remember to alias this script as SQUASH_LAST.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment