β git commit: automatically add pull request template
β’ 162 words β’ 1 min β’ updated
GitHub supports pull request
templates.
Define a .github/PULL_REQUEST_TEMPLATE.md directory in your git repo, and it
will be automatically used when creating PRs via the GitHub web UI and via the
CLI tool (gh).
But what about commits created locally, via a plain git commit?
As per git docs:
commit.templateSpecify the pathname of a file to use as the template for new commit messages.
Therefore, the following ~/.gitconfig works:
[commit]
template = .github/PULL_REQUEST_TEMPLATE.mdThe file path (pathname) is relative to the git repository root.
However, this will fail in repositories that do not have a template:
% git commit
fatal: could not read '.github/PULL_REQUEST_TEMPLATE.md': No such file or directoryThere’s a workaround: use includeIf:
% tail -3 ~/.gitconfig
# Import corp configs if any.
[include]
path = .gitconfig_corp[includeIf "gitdir:~/mycompany/**"]
path = .gitconfig_mycompany
[includeIf "hasconfig:remote.*.url:git@github.com:mycompany/**"]
path = .gitconfig_mycompany[commit]
template = .github/PULL_REQUEST_TEMPLATE.mdThis way, the github PR template is only included for git repositories from my company.