From https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit (with few changes on the end by me 😉)
# Switch to the master branch and make sure you are up to date.
git checkout master
git fetch # this may be necessary (depending on your git config) to receive updates on origin/master
git pull
# Merge the feature branch into the master branch.
git merge feature_branch
# Reset the master branch to origin's state.
git reset origin/master
git branch -D feature_branch && git checkout -b feature_branch
git add .
git commit -m 'yada yada'
git push origin feature_branch -f