Yes, you can do it…
When I work on a feature branch, I like to commit and push often so, some commits are nothing more than indications about the latest status of the feature and so they might not make sense when the feature is ready to be merged into parent branch via PR, that’s why is common to merge, or to better say ‘squash’ all commits into one.
If you’re a Git wizard you know that behind a squash there is indeed a rebasing operation (see here if you want to dig into this more) but since I’m not, i wanted to to it from inside Visual Studio using the Git plugin and luckily this feature has been introduced in Visual Studio 2019 release.
To see it in action, let’s say we’re on feat/task01 branch and we have submitted and pushed a series of commits during the development of assigned task as we can see from branch history
To squash commits into one we just have to select them, right click and select the Squash Commits menu.
and now give a meaningful name to the single merged commit and press Squash button.
and now the history gets rewrited this way
Now things get a little bit tricky if you don’t know that behind this operation there is a Git Rebasing operation because if you now try to push your squashed commit you will see that on remote branch there are two commits you have to pull first, this is because of the rebasing operation occurred ‘under the hood’.
In order to overcome this, go to plugin Global settings and enable push — option.
You will be now able to push to remote branch even in presence of remote commits.
Please note that this is a risky overwrite operation and it might lead to some remote commits being lost if you don’t pay required attention.
Note: If you’re using Azure DevOps you can also squash merge the PR.