Help Center> CodeArts Repo> FAQs> Using Repositories> How Do I Modify a Historical Commit Message?
Updated on 2023-12-04 GMT+08:00

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.
    1. 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.
    2. Run the following command to query the latest commit record.
      git log

    3. Run the following command to open the vi text editor.
      git commit --amend
    4. 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.
    1. Run the following command on the git bash to search for incorrect commit records.
      git log

    2. 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

    3. Modify the previous commit record by referring to 2 or 3, and then press Enter to save the modification.
    4. 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.

    5. Run the following command to select a branch.
      git cherry-pick commitID

      For example, the commitID is 62c6ac5bab09bb450007bb9996177ee711c7e2fb.

      git cherry-pick 62c6ac5bab09bb450007bb9996177ee711c7e2fb

      1. 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.
      2. If the commit fails, run the git push command to push the new one.
      3. 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.

Using Repositories FAQs

more