background

During development, there are countless local commits, and sometimes we want to merge the same commit to keep the history clean

git rebase

$git rebase -i [commitid] $git rebase -i [commitid]Copy the code

Parameter Description:

  • -i Enters the interactive mode
  • Commitid If you specify a commitid, only all submissions after the commitid are listed on the GUI

Interface command description:

# command: # p, pick < commit > = Use commit # r, reword < commit > = Use commit, but modify commit description # e, edit < commit > = Use commit, enter shell for commit patch # s, squash < commit > = Use commit, But to fuse to the previous commit # f, fixup < commit > = similar to "squash", Git rebase --continue -- git rebase -- git rebase --continue -- git rebase Drop < commit > = Drop the commitCopy the code

steps

To view the log

Git logs are new to old commits from top to bottom

$ git log --oneline 291e427 update website 8c8f3f4 update website 1693a6f update clear-logs.sh version 3759b84 update Sh fc36a2a add links 1d795e6 fix && update clear-logs.sh 0.0.2 9536dab add dingtalk script 3a51aaa fix shellcheck problem 2db6ad3 add clear logs scripts e57b0e6 fix && add batch del 17cb931 fix && add batch del cf7e875 add redis script fe4bbcb Initial commitCopy the code

Edit the version to merge

Cf7e875 does not participate in the merge, Enter the vi editor $git rebase -i cf7e875 pick 17cb931 fix && add batch del pick e57b0e6 fix && add batch del pick 2db6ad3 add clear logs scripts pick 3a51aaa fix shellcheck problem pick 9536dab add dingtalk script pick 1d795e6 fix && update Sh 0.0.2 pick fc36a2a add links pick 3759b84 update clear-logs.sh pick 1693a6f update clear-logs.sh version pick 8c8f3f4 update website # Rebase cf7e875.. 291e427 onto cf7e875 (10 commands) # # 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 # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented outCopy the code

Edit Commit Information

# merge e57b0e6 into 17cb931 Merge 1693A6F to 3759B84 pick 17CB931 fix && add Batch del f e57B0E6 fix && add Batch del pick 2DB6AD3 add clear logs scripts pick 3a51aaa fix shellcheck problem pick 9536dab add dingtalk script pick 1d795e6 fix && update clear-logs.sh 0.0.2 pick fc36a2a add links pick 3759b84 update clear-logs.sh s 1693a6f update clear-logs.sh version pick 8c8F3F4 update websiteCopy the code

Then :wq exits the vim editor. This is the comment interface

# This is a combination of 2 commits. # This is the 1st commit message: update clear-logs.sh # This is the commit message #2: update clear-logs.sh version # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Tue Jul 28 18:25:57 2020 +0800 # # interactive rebase in progress; onto cf7e875 # Last commands done (9 commands done): # pick 3759b84 update clear-logs.sh # s 1693a6f update clear-logs.sh version # Next command to do (1 remaining command):  # pick 8c8f3f4 update website # You are currently editing a commit while rebasing branch 'master' on 'cf7e875'. # # Changes to be committed: # modified: logs/README.md # modified: logs/clear-logs.shCopy the code

You can edit the commit message, save it, and exit (:wq) to complete the commit merge

update clear-logs.sh
Copy the code

View the merged log

$ git log --oneline 47e7751 update website 4c2316c update clear-logs.sh 73f082e add links 56adcf2 fix && update Sh 0.0.2 ebf3786 add dingtalk script 6e81ea7 fix ShellCheck Problem 64CA58f add clear logs scripts 9327def fix && add batch del cf7e875 add redis script fe4bbcb Initial commitCopy the code

You can then push the collated COMMIT to the remote

Conflict resolution

Git rebase -i resumeError: Could not apply… git rebase -i resumeerror: Could not apply… .

$git rebase --continue $git rebaseCopy the code