Git
Commit Merge
Rebase
Coding Challenges
Programming Solutions

How can I merge two commits into one if I already started rebase?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When working with Git, you might find yourself in a situation where you need to clean up your commit history before merging changes into another branch. One common task is to merge two or more commits into one. This is particularly useful for maintaining a clear and manageable commit log, especially when you are fixing minor issues or making small changes. Rebasing is a powerful Git feature that helps achieve this by modifying the commit history. If you've already started a rebase and want to merge commits, you can still do so during the rebase process.

Understanding Rebase

Rebasing is a process that rewrites the commit history by moving or combining a series of commits to a new base commit. It's often used to ensure that a feature branch can be integrated cleanly into the main branch. Rebasing allows you to edit commits, reorder them, remove them, or combine them (squash/fixup).

How to Merge Two Commits During a Rebase

Merging two commits into one during a rebase typically involves the squash or fixup commands provided by Git. Here’s how you can do it step by step.

Step 1: Start the Interactive Rebase

If you haven't already started the rebase, you can begin with the following command:

bash
git rebase -i HEAD~n

Replace n with the number of commits you want to work with. If you already started a rebase and are paused in the middle of it, you can skip this step.

Step 2: Pick and Squash Commits

You will be brought to an editor with a list of commits from your feature branch, each starting with the word pick. Each pick command tells Git to keep the commit as is.

To merge two commits into one, change the word pick on the commit you want to merge into another commit to squash or s. This tells Git to merge this commit with the previous one. Here’s an example:

plaintext
pick 01d1124 Add user login feature
squash 034ac2d Fix remember me function

In this example, the "Fix remember me function" commit will be merged into the "Add user login feature" commit.

If you prefer, you can use fixup instead of squash. The difference is that fixup discards the commit's log message, while squash allows you to combine commit messages.

Step 3: Rebase Continues

Once you have marked the commits to be squashed, save and close the editor. Git will rebase the commits according to your instructions. If you have used squash, Git will prompt you to edit the new commit message. If you used fixup, Git will continue with the rebase without prompting for a message edit.

Step 4: Finalize the Rebase

Continue the rebase process by resolving any conflicts that occur and proceeding with the rebase completion using:

bash
git rebase --continue

Repeat these steps until all conflicts are resolved and you have a clean rebase.

Summary Table: Key Commands and Descriptions

CommandDescription
git rebase -i HEAD~nStart an interactive rebase involving the last n commits
pickKeep the commit as is
squashMerge the commit with the previous one, editing the message
fixupMerge commit while discarding this commit's log message
git rebase --continueContinue rebasing after resolving conflicts or editing

Additional Tips

  • Backup Before Rebasing: It's a good idea to create a backup branch before starting a rebase, in case you need to revert to the original state.
  • Rebase Small Changes: To avoid complex conflicts and potential errors, try to rebase small changes instead of large chunks of commits.
  • Regularly Push to Remote in Feature Branch: This ensures changes are updated and shared, but avoid rebasing public branches frequently as it can disrupt other collaborators.

By using these steps, you can effectively merge commits into one during a rebase, helping maintain a clean and professional commit history in your Git repository.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions