I have a requirement to prepend "ticket:N" to commit messages, where N is the number of the ticket I'm working on. But I keep forgetting about the prefix and remember about it only 5-6 commits later, so --amend won't help. Is it possible to set some warning, so git will warn me every time I forget to add the prefix?
3 Answers
To make sure every commit message follows some standard form, you can use the commit-msg hook.
But if you want to edit the commit message of some commit that is not the most recent, you can do that too using git rebase -i, assuming you didn't push it yet.
You can use filter-branch in combo with --msg-filter to update a range of commits.
For example, If you want to prepend ticket:N to every commit message from HEAD to xxxxxx:
git filter-branch -f --msg-filter 'printf "ticket:N " && cat' xxxxxx..HEAD
You can also append to the commit message by simply reversing printf and cat:
git filter-branch -f --msg-filter 'cat && printf "ticket:N"' xxxxxx..HEAD
2 Comments
If you specifically want to add JIRA ticket numbers to your commits you can use this method https://tjdane.medium.com/add-a-jira-ticket-to-a-batch-of-old-commits-67557fb42d3e