How Do I Modify a Historical Commit Message?
Symptom
The commit message of a local commit is incorrect. The following error information is displayed:
Analysis
The error message "message of commit" is displayed, followed by a string of 40 characters consisting of digits and lowercase letters. This character string indicates that the commit message is incorrect.
The commit ID 1fbd34f77d1ddb73b73b84b878fd9edbece711f5 is incorrect. The commit message does not match the regular-expression: ^\[maven-release-plugin\]|[Trouble ticket number or requirement ticket number]\s*.+\W[Modified description]\s*.+\W[Modified reason]\s*.+\W[Modules] \s*.+\W[Modifier]\s*.+\W[Approver]\s*.+
Solution
- To modify the latest incorrect commit record, perform the following operations.
- In the root directory of the local code workspace, that is, the level where the .git hidden folder exists, right-click and choose git bash here from the shortcut menu.
- Run the following command to query the latest commit record.
git log
- Run the following command to open the vi text editor.
git commit --amend
- Enter git commit --amend -m ', enter the information in the first line, and press Enter. Enter the information in the second line, and repeat the process to the last line. Add the other single quotation mark at the end of the last line, and press Enter. The modification is successful.
- To modify a commit record that is not the latest one, perform the following operations.
- Run the following command on the git bash to search for incorrect commit records.
git log
- Run the following command to roll back to the previous commit record.
git reset --hard commitID
For example, run the following command.
git reset --hard 1fbd34f77d1ddb73b73b84b878fd9edbece711f5
- Modify the previous commit record by referring to 2 or 3, and then press Enter to save the modification.
- Find your commit record and run cherry-pick.
If the commit message belongs to you and you have not performed a merge operation, you can perform cherry-pick.
As shown in the following figure, the commit message in the green box can be cherry-picked, and that in the red box cannot.
- Run the following command to select a branch.
git cherry-pick commitID
For example, the commitID is 62c6ac5bab09bb450007bb9996177ee711c7e2fb.
git cherry-pick 62c6ac5bab09bb450007bb9996177ee711c7e2fb
- If the incorrect commit message has been merged into a repository, you need to run the git push -f command to forcibly push the new one.
- If the commit fails, run the git push command to push the new one.
- If the error is caused by someone else and the commit has been merged into a repository, no further modification is required. You can directly delete the commit rules of the repository and push the new commit message to the repository by running the git push command.
- Run the following command on the git bash to search for incorrect commit records.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot