Member-only story

Git Squash (All)

How to squash all your commits from a feature branch before merging.

Photo by Karina Vorozheeva on Unsplash

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

--

--

Daniel Ellis Research
Daniel Ellis Research

Written by Daniel Ellis Research

Research Software Engineer specialising in High-Performance Computing and Data Visualisation. — PhD in Atmospheric Chemistry and Masters in Theoretical Physics.

No responses yet