Member-only story
Git Squash (All)
How to squash all your commits from a feature branch before merging.
Nov 16, 2023
Taking the code from steve crozz, I converted this into a function to run in my shell. This creates a new branch and collapses all your commits into a one.
freebase() {
TOPIC=$(git branch | grep '*' | cut -d ' ' -f2)
NEWBASE="${1:-main}"
PREVSHA1=$(git rev-parse HEAD)
echo "Freebasing $TOPIC onto $NEWBASE, previous SHA-1 was $PREVSHA1"
echo "---"
git reset --hard "$NEWBASE"
git merge --squash "$PREVSHA1"
git commit
}
Execution of this involves navigating to your feature branch and typing
freebase