12k
All articles

Setting up a git commit template: a step-by-step guide

Set up a Git commit template step by step to write consistent, clear commit messages faster and improve code reviews across all repositories.

OpenReplay Team
OpenReplay Team
Setting up a git commit template: a step-by-step guide

Writing good commit messages can be hard when you’re moving fast. Without a structure, it’s easy to end up with vague, inconsistent commits that slow down code reviews and project tracking.

A Git commit template helps by providing a starting point every time you create a commit. It makes writing clear messages faster and more consistent.

In this article, you’ll learn exactly how to create and use a Git commit template to improve your commit quality without slowing down your workflow.

Key Takeaways

  • A Git commit template helps standardize your commit messages
  • Setting one up takes just a few minutes
  • Good commit messages make collaboration and debugging easier

What is a Git commit template?

A Git commit template is a pre-written file that appears whenever you run git commit without the -m flag. It provides a structured format or reminders for writing better commit messages.

Instead of starting from a blank screen, you see helpful prompts like:

# Commit title (50 characters or less)

# Description: Why is this change needed?

# Issue reference (optional):

You can edit these lines before finalizing your commit.

How to create a Git commit template

Step 1: Create a template file

Create a text file somewhere on your system, for example:

nano ~/.gitmessage.txt

Inside the file, add a simple structure:

# Title: (short summary, 50 characters max)

# Description:
# - Explain why this change is needed
# - Mention any context or background

# Related Issues:
# - Reference ticket numbers or links

Save and close the file.

Step 2: Configure Git to use the template

Tell Git to use this file as the commit message template:

git config --global commit.template ~/.gitmessage.txt

This applies the template across all your repositories.

Step 3: Start using it

When you create a commit without a message inline:

git commit

Git opens your default editor with the template loaded. You fill in the sections, save, and close the editor to finalize the commit.

If you prefer committing with -m inline messages for quick fixes, you can still do that. The template only appears when you omit -m.

A better template example

You can customize templates to fit your team’s workflow. Here’s a more detailed example:

# Type: [feat|fix|docs|refactor|test|chore]
# Scope: (optional module or file name)
# Subject: (short imperative description)

# Body:
# - Motivation and context
# - Link to related issues or tickets

# Footer:
# - Breaking changes
# - Related discussions

This approach matches common Conventional Commits standards.

Why use a Git commit template

  • Consistency: Your history will be much easier to read.
  • Speed: No need to think about format every time.
  • Better code reviews: Clear commits speed up peer review.
  • Easier debugging: You can trace bugs and changes more efficiently.

Related: How to create and use Git aliases for faster workflow

Conclusion

A Git commit template is a simple but powerful tool for improving code quality and team collaboration. By taking a few minutes to set one up, you’ll save time, reduce confusion, and write better commits from day one.

FAQs

Can I have different commit templates for different repositories?

Yes. If you set the template without `--global`, it will apply only to the current repository.

Does the commit template force a format?

No. It's just a starting point. You can delete, ignore, or customize it for each commit.

Can I change the default editor Git uses for commits?

Yes. You can set it by running `git config --global core.editor 'your-editor'`, for example `nano` or `vim`.

Listen to your bugs 🧘, with OpenReplay

See how users use your app and resolve issues fast.
Loved by thousands of developers

We use cookies to improve your experience. By using our site, you accept cookies.