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

Visual Studio Code, or VSCode, is known for its robust ecosystem of extensions that can enhance your development workflow. One such extension is GitLens, which supercharges the built-in Git capabilities of VSCode. In this guide, we’ll show you how to use GitLens’s interactive rebase feature to combine all your commits into a single commit directly within your editor.

Why Squash Commits?

Squashing commits with GitLens can help to:

  • Simplify your commit history before making a pull request.
  • Bundle together related changes for easier code review.
  • Clean up your commit log by discarding unnecessary intermediate commits.

Setting Up Your Environment

To get started, make sure you have the following:

  • Visual Studio Code installed on your machine.
  • The GitLens extension installed in VSCode.
  • A local Git repository with multiple commits that you’d like to squash.

Step 1: Open the Integrated Terminal

In VSCode, bring up the Integrated Terminal by using the shortcut:

  • Windows/Linux: Ctrl+``
  • macOS: Cmd+`

Ensure the Integrated Terminal is directed to the project directory.

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

Step 2: Finding the Commit Hash

You need to find the commit hash before the first commit you want to squash. You can use git log to display the commit history as shown below:

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

Step 2: Start Interactive Rebase

Start an interactive rebase with the parent of the first commit you want to squash:

git rebase -i <commit_hash>^

The caret ^ is used to indicate the parent of the commit hash.

Step 3: Select Commits to Squash

The interactive rebase interface will open up, allowing you to choose from various actions for each commit. For the commits you want to squash:

  1. Click on the commit immediately following the one you want as the start (the oldest commit you want to retain).
  2. From the dropdown options that appears, select “squash” with previous.
How to Combine All Commits into One with GitLens Interactive Rebase in VSCode

Repeat this for all subsequent commits that should be combined into the initial one and click on “START REBASE”.

Step 4: Edit Commit Messages

After selecting the appropriate ‘squash’ actions, a new tab will open up where you can edit the commit messages. This is your opportunity to craft a comprehensive message that summarizes the entire set of changes.

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

Once you’ve edited and saved the commit message to your satisfaction, close the editor tab to proceed.

Step 5: Complete the Interactive Rebase

With your new commit message in place, GitLens will continue with the rebase. If there are no conflicts, your commits will be squashed successfully. You can now view your simplified commit history in the Repositories view.

Step 6: Push the Changes

If you’re ready to share your new commit, you’ll need to force push to update the remote repository. As mentioned earlier, force pushing can disrupt shared branches, so proceed with caution.

In the VSCode terminal, enter the following command:

git push origin <your-branch-name> --force

Replace <your-branch-name> with the actual name of your branch.

Conclusion

Congratulations! You’ve just simplified your Git history using the GitLens extension in Visual Studio Code. By combining all of your commits into one with an interactive rebase, your project’s history is now concise and ready for others to review. Remember, clean commit logs facilitate easy collaboration and maintain the integrity of your project’s development timeline.

Happy coding, and enjoy the streamlined power of GitLens!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.