Git

How to Combine All Commits into One with GitLens Interactive Rebase in VSCode

Before opening a pull request, you'll often want to squash multiple commits into one clean commit. Instead of doing this entirely from the command…

How to Combine All Commits into One with GitLens Interactive Rebase in VSCode
Share:

How to Combine All Commits into One with GitLens Interactive Rebase in VSCode

5 min read
2.4K views

Before opening a pull request, you’ll often want to squash multiple commits into one clean commit. Instead of doing this entirely from the command line, you can use GitLens in VS Code to run an interactive rebase with a visual UI. This guide shows you how to combine all commits into one using GitLens interactive rebase.

The examples in this guide are run on WSL2 Ubuntu in Windows with VS Code connected via the WSL extension, but this works the same on any platform.

Advertisement

When to Squash Commits

You might need this when:

  • Your feature branch has “WIP”, “fix typo”, or “oops” commits that don’t belong in the main history
  • A reviewer asks you to clean up your commit history before merging
  • You want one meaningful commit per feature instead of 15 incremental ones

Prerequisites

  • VS Code with the GitLens extension installed (on WSL, install it in the WSL window so it runs on the Linux side)
  • Git installed. See How to Install the Latest Git on Ubuntu
  • Git configured to use VS Code as its editor, so git rebase -i opens inside VS Code: git config --global core.editor "code --wait"
  • A Git repository with multiple commits on your current branch that you want to combine

Step 1: Find the Base Commit Hash

First, identify the commit before the ones you want to squash. Open the VS Code integrated terminal (Ctrl+`) and run:

git log --oneline

This shows your commit history with short hashes. Find the commit that comes before your first commit to squash. Copy its hash — you’ll need it in the next step.

Advertisement

For example, if your branch has these commits:

a1b2c3d fix: button alignment
e4f5a6b add: login form validation
c7d8e9f add: login page UI
0a1b2c3 initial setup

To squash the top three commits into one, you need the hash of 0a1b2c3 (the commit before the ones you want to combine).

If you want to squash everything on your feature branch, let Git find the base for you. git merge-base returns the commit where your branch split off from main:

git merge-base main HEAD

If main has moved on since you branched, consider updating your feature branch with the latest main first, so you only squash your own work.

Step 2: Start the Interactive Rebase

Rewriting history is easy to undo if you leave yourself a marker. Create a backup branch first:

git branch backup-before-squash

Then run the interactive rebase command with the base commit hash:

git rebase -i 0a1b2c3

Or squash the whole branch in one go:

git rebase -i $(git merge-base main HEAD)

Or use the caret to reference one commit before your target:

git rebase -i c7d8e9f^

The ^ means “the parent of this commit”.

Because Git’s editor is set to code --wait and GitLens is installed, VS Code opens the GitLens Interactive Rebase Editor instead of a text-based editor. This is the visual UI where you’ll configure the squash.

If you see a plain-text git-rebase-todo file instead, run GitLens: Enable Interactive Rebase Editor from the Command Palette (Ctrl+Shift+P). If nano or vim opens in the terminal, core.editor isn’t set to VS Code yet (see Prerequisites).

Step 3: Squash the Commits in the GitLens UI

The GitLens rebase editor shows your commits in chronological order (oldest at the top). Each commit has a dropdown action that defaults to pick.

To squash all commits into the first one:

  1. Leave the first commit (oldest) set to pick — this is the one you’re squashing into
  2. For every subsequent commit, click the dropdown and change it from pick to squash
  3. Click the Start Rebase button at the top of the editor

The squash option combines a commit into the one above it while keeping both commit messages. If you don’t need the intermediate messages at all, use fixup instead — it squashes the commit and discards its message.

How to Combine All Commits into One with GitLens Interactive Rebase in VSCode

Step 4: Edit the Commit Message

After clicking Start Rebase, VS Code opens a commit message editor. If you used squash, it shows all the combined commit messages. Edit this into a single, clear commit message that describes the full change.

How to Combine All Commits into One with GitLens Interactive Rebase in VSCode

Save and close the file to complete the rebase.

If Something Goes Wrong

If the rebase stops on a conflict or you change your mind partway through, cancel it and return the branch to exactly where it was:

git rebase --abort

If the rebase already finished and the result is wrong, reset to the backup branch you created in Step 2:

git reset --hard backup-before-squash

Once you’re happy with the squashed commit, delete the backup with git branch -D backup-before-squash.

Step 5: Push the Squashed Commit

Since you rewrote the commit history, a normal git push will be rejected. You need to force push:

git push --force-with-lease origin your-branch-name

Use --force-with-lease instead of --force. It does the same thing but stops the push if someone else has pushed to the branch since your last pull — protecting you from overwriting their work.

Only force push to your own feature branches. Never force push to main or shared branches.

Alternative: Squash from the Command Line

If you prefer the terminal over the GitLens UI, you can do the same thing with a soft reset. Create the backup branch first, since a reset leaves no rebase to abort:

git branch backup-before-squash
git reset --soft $(git merge-base main HEAD)
git commit -m "add: login page with form validation"
  • git reset --soft — moves the branch pointer back to the base commit but keeps all changes staged
  • git commit — creates a single new commit with all the staged changes

This is faster if you don’t need to pick individual commits to keep or drop. If you have uncommitted changes you want to preserve first, see Safety Moving Uncommitted Changes to A New Branch in Git.

Conclusion

Your feature branch now has a clean, single commit ready for review. GitLens makes interactive rebase visual and less error-prone than editing the rebase todo file by hand. For other Git workflow tips, see How to Merge Branches in Git Without Auto-Resolving Conflicts and How to Configure Git Pushes Without Authentication Prompts.

Recommended for Linux builders

Contabo VPS hosting for Linux projects

Run Linux servers, automation tools, and self-hosted apps on a Contabo VPS with generous resources and predictable pricing.

Our customers sayExcellent4.6 out of 5 based on 11,008 reviews Trustpilot

Some links are affiliate links. This may earn LinuxBeast a commission at no extra cost to you.

Privacy settings

Choose which privacy controls to open.

Opening these controls does not change your saved choices.