I. Scene:
Sometimes when we change a Bug or a piece of code and commit it once, we find that the Bug was not corrected or the code needs to be optimized, and commit it once or more, and the commit history is not very concise. In this case, we want to keep only one commit history. What about merging into a full commit?
Introduction to Rebase
Rebase allows you to edit, delete, copy, and paste a linear commit history. Therefore, proper use of the rebase command can keep our commit history clean and concise! However, do not use rebase to modify any commit that has already been committed to a public repository (except for branches that you are playing alone). Merge is recommended if the commit is public
Three, specific operation
Git rebase -i < commit id >Copy the code
[startPoint] [endpoint] specifies an editing interval. If [endpoint] is not specified, if [endpoint] is not specified, By default, the end of the interval is the commit that the current branch HEAD points to. One thing to note here is that the commits you want to merge, the commit number after you -i, should be the commit before the earliest one of those commits. After viewing the log, we run the following command:
Git rebase -i HEAD~3 git rebase -i HEAD~3Copy the code
Then we will enter the interface as shown below:
Press I to enter insert mode, that is, you can edit mode, then press Esc to close editing, :wq to save and launch vi
The annotation part in the figure is the command description provided by Git. Git provides the following commands:
Pick: Keep the commit (p) reword: Keep the commit, but I need to modify the comment on the commit (R) Edit: Keep the commit, but I need to stop and modify the commit (not just the comment) (e) Squash: Exec: Execute shell command (x) DROP: discard the commit (d)Copy the code
After the final modification is complete, save and exit. Git log views the commit record changes.