DEV Community

Colby Garland
Colby Garland

Posted on

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.

Top comments (5)

Collapse
 
skaunov profile image
skaunov

This note really lacks instructions on continue after conflicts resolving. I have a stupid issue with commands, headings, and comments, so seeking how they wanted me to continue without typing, and have no idea what to click. =\

Collapse
 
bdougherty profile image
Brad Dougherty

Wouldn't it be easier to do a squash merge instead? git merge --squash [branch]

Collapse
 
brackcarmony profile image
Brack Carmony

It would, if the assumption that every commit in the chain is what you want, this lets you keep the power of the rebase available if you want to cherry-pick commits or any of the other crazy features it seems to let you use.

Collapse
 
salicius profile image
salicius

hi, there is no "Start Rebase" button, where can i find it ? (i'm on vscode)

Image description

Collapse
 
skaunov profile image
skaunov

I guess you're already rebasing, but with git (judging by active terminal name on the right); and this page describe GitLens: there in "commits" section select the one after which you want to massage the line, and don't forget about choosing the branches relevant to you.