Merge multiple commits using git rebase


In normal software development, each team uses a public warehouse (GIT warehouse). Whenever there is a new requirement, we will pull out a feature branch, and then develop and submit personal code on this feature branch.

I have a habit: in order to ensure the security of the code, I will submit the personal local warehouse code to the public warehouse before work every day. Due to various reasons, the code will be submitted to the personal warehouse several times in the development process every day, which leads to several records submitted at a time when the code is submitted to the public warehouse after work. In this way, the public warehouse has all kinds of messy incremental modification history. In order to avoid the confusion of version control caused by too many commits, we usually recommend merging these commits into one.

About git’s common commands, we recommend browsing git’s common commands

Merge and submit using rebase

There are two ways to merge n submission records

1. From the head version to the pastnVersions

git rebase -i HEAD~n

2. Specify a merge intervalstartpointandendpoint, note:This interval specifies an open before close interval, which means that startpoint does not participate in merging

git rebase -i  [startpoint]  [endpoint]
  • -iI mean--interactiveThat is, an interactive interface will pop up for users to edit and complete the merge operation
  • startpointandendpointAn edit interval is specified
  • If not specifiedendpoint, then the end of the intervalendpointThe default is the commit that the current branch head points to


1. Implementationgit logLooking at the submission history, let’s merge the three submissions into one submission

Merge multiple commits using git rebase

2. Implementationgit rebase -i HEAD~3, pop up the operation window

Merge multiple commits using git rebase

Operating instructions:

  • Pick: keep the commit (abbreviation: P)
  • Reword: keep the commit, but I need to modify the comment of the commit (abbreviation: R)
  • Edit: keep the commit, but I’ll stop to modify the commit (not just the comment) (abbreviation: e)
  • Square: merge the commit with the previous commit (abbreviation: s)
  • Fix up: merge the commit with the previous commit, but I don’t want to keep the comment information of the commit (abbreviation: F)
  • Exec: execute shell command (abbreviation: x)
  • Drop: I want to discard the commit (abbreviation: D)

According to our requirements, we edit the commit content as follows:

pick 85697ee This is first commit.
squash ee461c1 This is second commit.
squash 326e415 This is third commit.

The above means to merge the second and third submission into the first submission.

3. Then save and exit. Git will compress the submission history. If there is a conflict, we need to modify it. When we modify it, we should pay attention to keeping the latest history, otherwise our modification will be discarded

After modification, remember to type the following command:

git add .  

git rebase --continue  

If you want to give up this compression, execute the following command:

git rebase --abort

4. If there is no conflict or the conflict has been resolved, the following edit window will appear

Merge multiple commits using git rebase

We combine three submissions into one submission

Commit feature branch!

#This is first commit.
#This is second commit.
#This is third commit.

5. After editing and saving, you can complete the merge of commit. Let’s execute itgit log

Merge multiple commits using git rebase

Then submit it to the public warehouse. Do you feel that the submission record is refreshing? Come and have a try!

Welcome to my personal blog

Pay attention to the official account:Java 9:30 classHere is a group of excellent program ape, join us to discuss technology and progress together! Reply to “information” to get the latest information of 2T industry!