Git squash commits in VS Code

Posted by Danny Herran on Jun 18, 2020 in Other Stuff | No comments
Git push

If you are looking to squash several commits into one in VS Code without having to use the console, I have a solution for you.

First off you need the GitGraph plugin, which simply allows you to see the full git log history of your branches. This is handy when having to interact with specific commits, which we will do if we want to squash them.

Once installed, go to the GitGraph log (you can do this from the source control sidebar or by pressing F1) and proceed as follows:

  1. Right-click the commit that is previous to the one you want to keep. For example, if I wanted to squash all the commits in red, I would right-click the commit in green, then select “Reset current branch to this commit…”
  1. You will then see to your left the unstaged changes belonging to all the commits you had before. Proceed to “Commit All…”
  1. Finally, do a force push to origin to replace all your existing commits for a single one.

Be reminded that this works for branches where you are the only collaborator. If your branch is shared with multiple developers and you do a force push, be prepared to bring a box of donuts to the office next morning.😂

Another alternative is to “Reset” your commit to the first commit you made to your branch and then do a “Commit Amend”. It does pretty much the same, so it’s just a matter of preference.

If you don’t have the “Push (force)” option in VS Code, make sure you go to “Settings > Extensions > Git > Allow Force Push”.

Please don’t do this on master. Just don’t.