git
squash
commit

How do I squash my last N commits together?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

To squash the last N commits together in Git, you can use an interactive rebase. This process allows you to combine multiple commits into a single commit. Here’s how you can do it:

Steps to Squash Last N Commits

  1. Start an Interactive Rebase:
    Run the following command to start an interactive rebase for the last N commits:
bash
 git rebase -i HEAD~N

Replace N with the number of commits you want to squash. For example, if you want to squash the last 3 commits, use:

bash
git rebase -i HEAD~3
  1. Mark Commits for Squashing:
    After running the rebase command, an editor will open showing a list of the last N commits. It will look something like this:
plaintext
 pick commit1_hash Commit message 1
 pick commit2_hash Commit message 2
 pick commit3_hash Commit message 3
  • The first commit should remain as pick.
  • For the commits you want to squash into the first one, replace pick with squash (or simply s).

For example:

plaintext
pick commit1_hash Commit message 1
squash commit2_hash Commit message 2
squash commit3_hash Commit message 3

This tells Git to squash the second and third commits into the first one.

  1. Save and Close the Editor:
    After making your changes, save and close the editor. In Vim, you would press Esc, type :wq, and then press Enter.
  2. Combine Commit Messages (Optional):
    After squashing, another editor window may open, allowing you to edit the commit message for the combined commit. You can choose to edit, combine, or replace the commit messages.
    • The default behavior is to concatenate all the commit messages.
    • You can modify this as needed. Save and close the editor when you’re done.
  3. Force Push to Remote (If Necessary):
    If you’ve already pushed these commits to a remote repository, you’ll need to force push the changes to update the remote branch:
bash
 git push origin <branch-name> --force

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

Example Workflow

Let’s say you want to squash the last 3 commits on the main branch:

  1. Start the rebase:
bash
 git rebase -i HEAD~3
  1. Edit the rebase instructions:
    Change it from:
plaintext
 pick abcdef1 Commit message 1
 pick abcdef2 Commit message 2
 pick abcdef3 Commit message 3

To:

plaintext
pick abcdef1 Commit message 1
squash abcdef2 Commit message 2
squash abcdef3 Commit message 3
  1. Save and close the editor.
  2. Edit the combined commit message if needed, then save and close the editor.
  3. Force push if the branch was previously pushed:
bash
 git push origin main --force

Summary

  • git rebase -i HEAD&#126;N: Start an interactive rebase for the last N commits.
  • Mark commits with squash: Change pick to squash for the commits you want to combine.
  • Force push: Use git push --force to update the remote branch if necessary.

By following these steps, you can successfully squash your last N commits into a single, cleaner commit.


Course illustration
Course illustration

All Rights Reserved.