How to squash commits in git after they have been pushed?
Solution 1:
Squash commits locally with
git rebase -i origin/master~4 master
and then force push with
git push origin +master
Difference between --force
and +
From the documentation of git push
:
Note that
--force
applies to all the refs that are pushed, hence using it withpush.default
set tomatching
or with multiple push destinations configured withremote.*.push
may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a+
in front of the refspec to push (e.ggit push origin +master
to force a push to themaster
branch).
Solution 2:
On a branch I was able to do it like this (for the last 4 commits)
git checkout my_branch
git reset --soft HEAD~4
git commit
git push --force origin my_branch