How Can I Merge Two Commits Into One if I Already Started Rebase?

Better Stack Team
Updated on February 26, 2025

If you've started a rebase and want to merge two commits into one, you can use an interactive rebase to combine them. Here's a step-by-step guide to do this:

Steps to Merge Two Commits into One During an Interactive Rebase

  1. Start an Interactive Rebase

    First, determine how many commits back you need to go. For example, if you want to merge the last two commits, you would start the rebase from two commits ago.

     
    git rebase -i HEAD~2
    

    This command will open an interactive rebase session for the last two commits.

  2. Modify the Rebase Instructions

    In the editor that opens (typically your default text editor), you'll see a list of commits. The most recent commit will be at the top.

    The file might look something like this:

     
    pick abcdef1 Commit message 1
    pick abcdef2 Commit message 2
    

    Change pick to squash (or s) for the second commit. You should leave the first commit as pick. For example:

     
    pick abcdef1 Commit message 1
    squash abcdef2 Commit message 2
    

    This tells Git to squash the second commit into the first commit.

  3. Save and Close the Editor

    After modifying the rebase instruction, save and close the editor. This will start the rebase process.

  4. Edit the Commit Message

    After squashing, Git will open another editor window to allow you to edit the commit message for the combined commit. You can modify this message to describe both commits or leave it as it was.

    The editor might look like this:

     
    # This is a combination of 2 commits.
    # The first commit's message is:
    Commit message 1
    
    # This is the commit message of the second commit:
    Commit message 2
    

    Edit the commit message as needed. Remove or modify any unnecessary lines, then save and close the editor.

  5. Complete the Rebase

    Git will complete the rebase and merge the two commits into one. If there are no conflicts, the rebase process will finish, and the two commits will be combined into a single commit with the updated message.

  6. Handle Conflicts (if any)

    If there are conflicts during the rebase, Git will pause and prompt you to resolve them. You’ll need to:

 
- Resolve the conflicts in your files.
- Stage the resolved files with `git add`.
- Continue the rebase with `git rebase --continue`.

If you decide to abort the rebase, you can use:

```
git rebase --abort
```

Example Scenario

Assume you have the following commit history:

  • Commit A: Initial commit
  • Commit B: Added feature X
  • Commit C: Fixed bug in feature X

You want to merge commits B and C into one commit. Here’s what you would do:

  1. Start Interactive Rebase:

     
    git rebase -i HEAD~2
    
  2. Modify the Rebase Instructions:

    Change the file from:

     
    pick abcdef1 Added feature X
    pick abcdef2 Fixed bug in feature X
    

    To:

     
    pick abcdef1 Added feature X
    squash abcdef2 Fixed bug in feature X
    
  3. Save and Close the Editor.

  4. Edit the Commit Message.

    For example:

     
    Added feature X
    
    Fixed bug in feature X
    
  5. Complete the Rebase.

Summary

To merge two commits into one during an interactive rebase:

  1. Start an interactive rebase: git rebase -i HEAD~n (where n is the number of commits back).
  2. Change pick to squash (or s) for the commit you want to merge.
  3. Save and close the editor.
  4. Edit the commit message for the combined commit.
  5. Save and complete the rebase.

If conflicts occur, resolve them and continue the rebase with git rebase --continue. If necessary, you can abort the rebase with git rebase --abort.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.