Created
October 23, 2025 21:21
-
-
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.
This file contains hidden or 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
| 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