Table of contents
Modify several Commits in the past
Scenario: When submitting multiple times in batches, it is suddenly found that the submitted message does not conform to the specification, and you want to modify it, then you can use it
Modify the latest Commit
If you just want to fix the latest Commit, use the command directly:
git commit --amend
You can modify it, and the command line will enter the vim interface, allowing you to modify the last submitted Message, change the message, and exit. Then force push (git push origin branch name -f).
Modify several Commits in the past
If you want to modify several Commit messages in the past, it will be a little troublesome whether you change the latest one or not.
For example, I have a requirement and submitted three commits. First, execute git log to view the commit records:
I want to modify the message submitted for the first time, plus the number of times
At this time we need to use the following command
git rebase -i 9c8b8a837dc2d831983339924a6eba91ba9a77d3
Entered the Vim interface:
Parameter Description:
- pick(p) : keep the commit
- reword(r) : keep the commit, but I need to modify the message of the commit
- edit(e) : Keep the commit, but I want to stop and modify the commit (including modifying the file)
- squash(s) : Merge the commit with the previous commit (merge multiple commits into one)
- fixup(f) : Merge this commit with the previous commit, but I don't want to keep the comment information of this commit
- exec(x) : Execute shell commands
- drop(d) : discard this commit
Select the command according to the actual needs. What we need here is reword, which is used to modify the Message.
Remember, if you need to change multiple records here, just change the pick in front of the corresponding commit id to r, and here I only change one.
After the modification is completed (there is no need to modify the message here), press esc, and then enter: wq to save and exit.
Then you will come to the submission page of d79776c, just edit the message
Press esc in the same way, and then enter: wq to save and exit.
If you modify multiple items, you will enter the submission page of each commit in turn, and edit the messages in turn. After the last edit is completed, it will output:
Successfully rebased and updated refs/heads/master.
It means that the rebase is successful, let's look at the git log again:
Don't forget to git push origin master (your branch name) -f oh~