Suppose you are working on a feature branch, and because of context-switching, your feature branch contains lots of temporary commits:
Before merging your changes to the main branch, you would like to squash all your changes to a single commit:
With this shell script, you can run:
squash-commits main 'feat: Add new header'
It will squash all of your commits to a single commit with the commit message feat: Add new header
right above the main
branch!
-
Download the squash-commits.sh file to your local computer, and name the file squash-commits.sh
-
Create an alias of this script on your terminal by having:
alias squash-commits="~/Downloads/squash-commits.sh"
in your
~/.bash_rc
or~/.zshrc
file -
Reload your terminal by running
source ~/.bash_rc
orsource ~/.zshrc
-
Go to your Git repo, check out to your feature branch, and run:
squash-commits <base-branch> "<your-commit-message>"
where:
<base-branch>
is the branch you want the single commit to be right above<your-commit-message>
is your commit message from your code changes
I have no responsibility over any damages or loss that may occur during the execution of or usage of my shell script.