Using VS Code to git rebase

colbygarland

Colby Garland

Posted on July 20, 2021

Using VS Code to git rebase

Git rebase is a powerful, albeit confusing tool. I primarily use rebase to squash my commits before I PR my branch into main. This tutorial will show you how to use VS Code to visually do this process.

Prerequisites

You will need to install this VS Code extension: https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens

You will also need to enable "force push":

In settings, search for "git allow force push" and check the box

image

Optional but recommended: set VS Code as your git editor:

git config --global core.editor "code --wait"

Let's Begin!

  1. Ensure your local branch you are rebasing onto (in our case, main) is up to date.
  2. Switch to your feature branch (the branch you have been working on)
  3. Type in git rebase -i main, where main is the branch you are rebasing onto
  4. You will then see the codelens rebase screen, where you can choose what to do with each commit. We are going to squash every commit, and reword the last one into a good commit message.
  5. Once you have your commits ready, we can click the Start Rebase button.
  6. You will then be presented with a screen in VS Code to reword your commit message. Write your new message, and save and close the file to continue.
  7. After this, our rebase is complete. However, now we must force push to remote. In your version control panel, click on the kebab menu (3 dots) and under the Pull, Push menu, choose Push (Force). Use this with caution, especially when working with other developers, as force push is destructive.

And we are done! A very easy way to rebase and squash all your commits using VS Code.

💖 💪 🙅 🚩
colbygarland
Colby Garland

Posted on July 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related