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
Optional but recommended: set VS Code as your git editor:
git config --global core.editor "code --wait"
Let's Begin!
- 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.
Top comments (6)
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. =\Wouldn't it be easier to do a squash merge instead?
git merge --squash [branch]
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.
Thank you! great idea to rebase onto main, I was counting the number of commits I need to go back to before...
hi, there is no "Start Rebase" button, where can i find it ? (i'm on vscode)
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 thebranch
es relevant to you.