How can I merge two commits into one if I already started rebase?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Merging two commits into one during an interactive rebase is a common task in Git, often used to clean up commit history before pushing changes. Rebasing is a way to integrate changes from one branch into another by applying changes one by one. Here's a guide on how to achieve this with step-by-step instructions, technical explanations, and illustrative examples.
Understanding Git Rebase
Git rebase is a powerful tool in version control that helps streamline a sequence of commits. It is often used to:
- Update a feature branch with the latest changes from the main branch.
- Clean up commit history by merging multiple commits into a single logical unit.
- Allow for an easier and cleaner conflict resolution process.
Prerequisites
Before starting the rebase process, ensure you have the necessary backup of your work. A rebase involves altering commit history, which can be destructive if mistakes are made.
Steps to Merge Two Commits During a Rebase
Step 1: Start Interactive Rebase
First, ensure you are on the branch where you want to perform the rebase. Begin an interactive rebase session targeting the commit just before the first commit you want to modify:
Here, replace n with the number of recent commits you want to review, considering the ones you intend to merge. For instance, if you're merging the most recent two commits, you'd use HEAD~2.
Step 2: Modify the Rebase TODO List
Git will open the default text editor displaying a list of commits:
To merge these two commits, change the word pick to squash or s for the second commit:
In this context:
pick- Retain the commit as is.squash- Merge the specified commit into the previous one.
Step 3: Edit Commit Messages
After saving and closing the editor, Git will pause to allow editing the commit message for the merged commit:
Edit the messages as necessary to create a cohesive and clear commit message for the new combined commit.
Step 4: Continue the Rebase
Once you are satisfied with the new commit message, save and close the editor. Git will continue applying the remaining commits and finalize the rebase.
Step 5: Resolve Any Conflicts
If there are conflicts, Git will pause and allow you to resolve these. After resolving, you would continue the rebase with:
Step 6: Verify the Merge
Once the rebase completes successfully, verify the changes with:
Check the history to ensure the commits have been successfully merged.
Potential Issues and Considerations
- Backup Your Work: Rebasing can rewrite history destructively; ensure you have backups or a way to retrieve commits if needed.
- Avoid Rebasing Shared History: Do not rebase commits that have been pushed and shared with others unless absolutely necessary and with all parties informed.
- Understand Conflict Resolution: Rebase can often lead to conflicts that must be resolved manually, understand how to manage and resolve these efficiently.
Key Points Summary
| Action | Description |
| Start Rebase | git rebase -i HEAD~n |
| Edit Rebase List | Change pick to squash/s to merge commits |
| Edit Commit Message | Combine commit messages for clarity |
| Manage Conflicts | Resolve conflicts and continue with git rebase --continue |
| Verify Changes | Use git log to check that commits are merged as expected |
By following these steps, you can clean up your commit history by merging multiple commits into one during an interactive rebase session. This results in a more organized and understandable project history, especially before integrating changes into shared branches.

