How to squash commits which have merge-commit in between

If you merge master onto your feature branch without rebasing it, and push the changes, then your branch may get cluttered. And also, your pull-request might show files that were not modified by you: the files that were edited again in master after your merge. That can be fixed by squashing all the previous commits with the help of a temporary branch:

  1. git checkout -b temp master
  2. git merge --squash feature
  3. git commit
  4. git checkout feature
  5. git reset --hard temp
  6. git branch -d temp
  7. git push --force

This way you get only one commit and your pull-request will show only files that were modified by you. I have found this solution in Stack Overflow, and it helped me with one of my tasks.

1 Like