Squash commits in Visual Studio

Corrado Cavalli
Jan 14 · 3 min read

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

Let’s check branch history
The two added commits

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.

Corrado Cavalli

Online thoughts of a technology funatic

Corrado Cavalli

Written by

Microsoft MVP mad about technology. MTB & Ski mountaineering addicted.

Corrado Cavalli

Online thoughts of a technology funatic

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade