git rebase: merge multiple commits into one

git rebase: merge multiple commits into one

Prerequisite: Multiple commits have been made on a branch, and each time they are pushed to the remote warehouse

When developing, pull a branch from the main branch for code writing. Sometimes in order to save the current work, it will commit multiple times, resulting in many requests in the online warehouse. It is best to combine multiple requests into one when merging branches. The following specific steps:

1. git log view all commit branches

  • Only part of it may be displayed. Pressing the Enter key will continue to display the remaining part until the first submission record appears
  • Press the letter q when you exit

2.git rebase -i commitID merge branch

  • The commitID here chooses the commitID that was submitted for the first time and the previous one, because it will not merge the commit of the commitID you filled in, but merge all the commits after it. If you choose the commitID of the first commit, it will only be merged from your second commit (the first commit of this branch is recorded as the first time, and the most recent one is recorded as the last time)

3. After entering, change everything except the first pick to f. Save and exit

  • After entering, press the letter i to enter the editing mode. Move the keys to modify. After modifying, press Esc to exit the editing mode, and press :wq to exit (the colon cannot be missing).

4.git push -f force push to the online warehouse

  • After this step, go to the online warehouse of the current operating branch and refresh it to see that one commit has been synthesized many times before. The name shows that it was submitted by the first commit. The following content is the sum of many previous submissions.


Regarding the third step of the merger method:

 git rebase -i commitID     commitID   commit ID
 git rebase -i HEAD~3       HEAD~num  num 3