Friday, December 25, 2020

Git changing the Message associated with a Commit

Before pushing code, many Git repositories require that the Git commit message contain an ID number for issue associated with the commit. For many projects this means the Git commit message should contain the associated Jira ticket number. A too common issue is when a commit is performed a developer forgets the issue ID or enters the issue ID in the wrong format. The solution is to edit the commit message.

To demonstrated, consider the following git commit:

git commit -m "SWP123 Migrated to Groovy/Jenkins build"

The Jira ticket should take the form of SWP-123 not SWP123 so the git push will fail for the above commit.

The Git command that allows the commit message to edited is git commit with the --amend option:

Invoking git commit  --amend displays the following which by default is the commit message and a  few lines of documentation loaded in the VIM editor (the default editor associated with git commands is VIM):

Using the intuitive editing commands of the the VIM editor (i to insert, edit the message, ESC to stop inserting, and :wq to save and quit) the Git comment can be modified:

The prefix of the git commit message was edited from SWP123 to SWP-123 which allows the git push to succeed since the commit message matches a legitimate Jira ticket number.


No comments :

Post a Comment