Yes, this is a good idea and fairly standard (but not universal) practice.
The specific software engineering goal you are achieving with this is requirements traceability. The idea is you want to be able to trace a requirement through the entire software process:
- Business requirements
- Functional requirements
- Technical requirements
- Code artifacts
- QA feedback
- Development fixes
By using ticket or requirement numbers (e.g. Jira story IDs) in commit messages and any correspondence, you are working toward that software engineering goal.
If I come back a year later and see the commit message, I can look up that number in another system to get the full background behind the requirement or ticket, including anything that occurred after the commit.
Answer from user22815 on Stack Exchangeversion control - Is there any downside to commit messages containing the ticket number - Software Engineering Stack Exchange
Customize commitlint with conventional commit + JIRA Ticket - Stack Overflow
githooks - Append ticket number using git commit hooks? - Stack Overflow
git - Including issue code in the commit message header according to Conventional Commits - Stack Overflow
Yes, this is a good idea and fairly standard (but not universal) practice.
The specific software engineering goal you are achieving with this is requirements traceability. The idea is you want to be able to trace a requirement through the entire software process:
- Business requirements
- Functional requirements
- Technical requirements
- Code artifacts
- QA feedback
- Development fixes
By using ticket or requirement numbers (e.g. Jira story IDs) in commit messages and any correspondence, you are working toward that software engineering goal.
If I come back a year later and see the commit message, I can look up that number in another system to get the full background behind the requirement or ticket, including anything that occurred after the commit.
The downside is that people will write less complete commit comments because someone can go to the ticket for more details. This is only really a problem if you say switch to a different ticketing system and can't keep the history or someone doesn't have ticketing system but does have access to the repository.
If the branch is already named for the ticket I wouldn't bother putting it in the commit, that does seem redundant.
You missed a hook. The one you want is commit-msg:
This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes a single parameter, the name of the file that holds the proposed commit log message. Exiting with non-zero status causes the git commit to abort.
So for example:
#!/bin/sh
ticket=$(git symbolic-ref HEAD | awk -F- '/^issue-/ {print $2}')
if [ -n "$ticket" ]; then
echo "ticket #$ticket" >> $1
fi
That's a very naive parsing of your branch name, and it's simply appended to the commit message on its own line. Modify it if that's not good enough for you.
Of course, I'd actually recommend doing this in prepare-commit-msg, and committing with git commit (without -m). It's very, very rare that you can actually write sufficient information in a single-line commit message. Further, that will let you see the message before the commit is made, in case your hook doesn't do quite what you want.
You can as well use prepare-commit-msg hook, which accepts more parameters than commit-msg. Then you can check if the message is coming from a file, a template, etc to avoid appending the issue numbers when you don't want it.
With the following script in .git/hooks/prepare-commit-msg when you are working in a feature branch named foo-123, then [#123] will be added to the third line of every commit you make.
More information in this post I wrote
#!/bin/sh
if [ x = x${2} ]; then
BRANCH_NAME=$(git symbolic-ref --short HEAD)
STORY_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*-\([0-9]\)/\1/p')
if [ x != x${STORY_NUMBER} ]; then
sed -i.back "1s/^/\n\n[#$STORY_NUMBER]/" "$1"
fi
fi