Merge multiple commits using Git rebase

1. The background

A REPO is usually maintained by multiple people on a team, and if a new feature needs to be added, it is a feature branch. Due to various modifications during development, this feature branch was committed several times. When you finally commit the master, you see a jumbled history of all the incremental changes. In fact, to others, our changes should be added or removed, showing others the development process of the increment is too messy. So we can merge the commit of the feature branch and then merge it into the trunk to make it look cleaner.

Remember that there was a post on the website asking why the author of vUE, Yu Da, was so clean in his submission history when developing vUE. How are Git commits so refreshing in history? – zhihu

2. Rebase profile

The functions of Rebase can be briefly summarized as: you can edit, delete, copy and paste a linear submission history; Therefore, proper use of the rebase command can keep our commit history clean and concise!

But here’s a caveat:

Do not use Rebase to modify any commit that has been committed to the public repository (except for branches that you play alone)

3. Negative examples

Create a new repo Rebase-test; Create dev branch; Commit three times on the development branch and merge into the master branch; And then git log or git log –oneline; You can see that every commit on the dev branch is reflected on the master

Fb28c8d (HEAD -> master, origin/master, origin/dev, origin/HEAD, dev) fix: Commit 26BAC61 Initial COMMITCopy the code

If you use Git log, press s to scroll down the log

Git log –oneline can be displayed in oneline

4. Specific operations

When we commit multiple times in the local repository, before we push the local commit to the public repository, we want to make the commit record more concise by combining the following three commit records B, C, and D into one complete commit and then pushing it to the public repository.

Here we use the command:

git rebase -i  [startpoint]  [endpoint]
Copy the code

[startPoint] [endpoint] specifies an editing interval. If [endpoint] is not specified, By default, the end of the interval is the commit that the current branch HEAD points to. After viewing the log, we run the following command:

git rebase -i 36224db
Copy the code

or

git rebase -i HEAD~3 

Copy the code

We should then see the following screen:

The uncommented section above lists all the commits we included in the rebase operation, and the commented section below is the command description git provided for us. Git provides the following commands:

Pick: keep the commit (abbreviated :p)

Reword: Keep the commit, but I need to modify the comment for the commit (abbreviated: R)

Edit: Keep the commit, but I stop to modify the commit (not just the comment) (abbreviated :e)

Squash: Merges this COMMIT with a previous commit (abbreviated: S)

Fixup: Merge this commit with the previous commit, but I don’t want to keep the comment information for this commit (abbreviated :f)

Exec: execute shell command (abbreviation :x)

Drop: I want to discard the commit (abbreviated :d)

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

Pick d2CF1f9 fix: first commit

S 47971f6 fix: The second submission

S Fb28C8d fix: The third submission

The above means to merge the second and third commits into the first

Then save the wq and exit the comment modification interface:

You can browse again by pressing two DD’s to delete a line

The final editing effect is as follows:

Commit merge

Finally the view log can be sent to commit merge

5. References

Rebase usage summary