Assuming the feature branch is called feature and the main branch main:
Create a temporary branch from main:
git checkout -b temp main
Squash the feature branch in:
git merge --squash feature
Commit the changes (the commit message contains all squashed commit messages):
git commit
Go back to the feature branch and point it to the temp branch:
git checkout feature
git reset --hard temp
Delete the temporary branch:
git branch -d temp