25
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.
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
Optional but recommended: set VS Code as your git editor:
git config --global core.editor "code --wait"
- Ensure your local branch you are rebasing onto (in our case,
main
) is up to date. - Switch to your feature branch (the branch you have been working on)
- Type in
git rebase -i main
, wheremain
is the branch you are rebasing onto - 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, andreword
the last one into a good commit message. - Once you have your commits ready, we can click the
Start Rebase
button. - 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.
- 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, choosePush (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.