thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

β˜… git commit: automatically add pull request template

β€’ 162 words β€’ 1 min β€’ updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

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.template

Specify 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.md

The 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 directory

There’s a workaround: use includeIf:

shell
% tail -3 ~/.gitconfig
# Import corp configs if any.
[include]
        path = .gitconfig_corp
~/.gitconfig_corp ini
[includeIf "gitdir:~/mycompany/**"]
  path = .gitconfig_mycompany

[includeIf "hasconfig:remote.*.url:git@github.com:mycompany/**"]
  path = .gitconfig_mycompany
~/.gitconfig_mycompany ini
[commit]
        template = .github/PULL_REQUEST_TEMPLATE.md

This way, the github PR template is only included for git repositories from my company.