5

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 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Are the hooks working every time, no matter where I call them IDE or some GUI tool?
I think they should, but I didn't try it.
4

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

A slight problem with this approach is that it'll add a newline between the prefix and the rest of the message. You can avoid this by using 'printf "ticket:N " && cat' as the filter instead. (echo -n isn't very portable and is only available in some shells).
This answer saved me! My specific problem was adding JIRA ticket numbers. This answer is the basis for the solution tjdane.medium.com/…
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.