Skip to content

Instantly share code, notes, and snippets.

@OscarL
Created October 23, 2025 21:21
Show Gist options
  • Select an option

  • Save OscarL/44dff94b7d7eb11c2ba61a03ab874a17 to your computer and use it in GitHub Desktop.

Select an option

Save OscarL/44dff94b7d7eb11c2ba61a03ab874a17 to your computer and use it in GitHub Desktop.
Basic bash function (place it on profile or bashrc) to quickly squash the N last commits.
squash_last_n_commits()
{
if [ $# -ne 1 ] || [ "$1" -lt 2 ]; then
echo 'squash_last_n_commits() requires a number (>1) as the only argument ("HEAD~$1")'
else
echo "Creating '__pre_squash__state__' tag, just in case."
git tag "__pre_squash__state__"
git reset --soft HEAD~$1 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"
if [ $? -ne 0 ]; then
echo "Squash operation aborted. Resetting to previous state."
git reset "__pre_squash__state__"
fi
git tag -d "__pre_squash__state__"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment