Merging Commits with Git Rebase

Git is a powerful version control system that allows developers to manage changes to their codebase efficiently. One of the essential features of Git is its ability to rebase, which enables you to modify and squash commits. In this tutorial, we will explore how to merge two or more commits into one using Git rebase.

Understanding Git Rebase

Before diving into the process of merging commits, it’s crucial to understand what Git rebase is and how it works. Git rebase is a command that allows you to modify your commit history by replaying your changes on top of another branch. This can be useful for cleaning up your commit history, squashing minor commits, or rearranging the order of your commits.

Merging Commits with Interactive Rebase

To merge two or more commits into one using Git rebase, you will need to use the interactive rebase mode. Here’s a step-by-step guide on how to do it:

  1. Start an interactive rebase: Use the command git rebase -i HEAD~n, where n is the number of commits you want to modify.
  2. Edit the commit list: In the editor that opens, you will see a list of commits with their corresponding SHA-1 hashes and commit messages. To merge two or more commits into one, change the pick command to squash (or s) for the commits you want to merge.
  3. Save and quit: Save the changes and exit the editor.

Example Use Case

Suppose you have a branch with three commits: A, B, and C. You want to merge commits B and C into one commit. Here’s how you can do it:

$ git log --pretty=oneline
a931ac7c808e2471b22b5bd20f0cad046b1c5d0d c
b76d157d507e819d7511132bdb5a80dd421d854f b
df239176e1a2ffac927d8b496ea00d5488481db5 a

$ git rebase -i HEAD~2

In the editor, change the pick command to squash for commit C:

pick   b76d157 b
squash a931ac7 c

# Rebase df23917..a931ac7 onto df23917
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Save and quit the editor. Git will then open another editor for you to edit the commit message of the new merged commit.

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

b

# This is the 2nd commit message:

c

Edit the commit message as desired, save, and quit. The two commits will now be merged into one.

Alternative Method: Using git reset --soft and git commit --amend

If you only want to merge the last two or more commits into one, you can use the following method:

$ git reset --soft HEAD~n
$ git commit --amend

This will softly un-commit the last n commits, and then you can amend the commit message using git commit --amend.

Important Notes

When rebasing, keep in mind that it rewrites your commit history. If you have already pushed your branch to a remote repository, you may need to use the --force option when pushing again. Additionally, be cautious when rewriting published history on a branch that others are working on, as this can cause conflicts and disrupt their workflow.

Conclusion

In conclusion, merging commits with Git rebase is a powerful feature that allows developers to manage their commit history efficiently. By using interactive rebase or the alternative method of git reset --soft and git commit --amend, you can easily merge two or more commits into one. Remember to be mindful of the implications of rewriting your commit history, especially when working with others.

Leave a Reply

Your email address will not be published. Required fields are marked *