Git Commit Template & Conventional Commits: Stop Turning Your Git History Into a Trash Heap

Git tutorial - IT technology blog
Git tutorial - IT technology blog

Instant Setup: 3 Steps to a Pro Commit Template

If you’re in a hurry and want a professional project immediately, implement this template in less than 60 seconds:

Step 1: Create a template file in your home directory (or directly in your project):

touch ~/.gitmessage

Step 2: Paste this standardized content into the .gitmessage file:

<type>(<scope>): <subject>

# --- COMMIT TYPES ---
#   feat:     New feature
#   fix:      Bug fix
#   docs:     Documentation update
#   style:    Code formatting (no logic change)
#   refactor: Code restructuring (neither a fix nor a feature)
#   perf:     Performance optimization
#   test:     Add unit tests
#   chore:    Update build tools, dependencies...
# --------------------------
# <scope>: Affected module (e.g., auth, api, ui...)
# <subject>: Brief description (under 50 characters)

Step 3: Enable the template globally:

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

From now on, every time you run git commit, a guide will appear in your editor. This helps prevent “writer’s block” when you’re unsure what to write.

Why Sloppy Commits Are a Recipe for Disaster

Recall the last time you ran git log only to find a long list of meaningless messages like “fixed,” “update,” or “asdfghj.” A total nightmare, right?

Early in my career, I once spent 4 hours just trying to find why a price calculation function was changed. The reason? The commit message simply said “fix.” In a project with over 1,000 files, searching through hundreds of “anonymous” commits is like finding a needle in a haystack.

When working in a team, being lazy with commit messages doesn’t just frustrate your colleagues—it kills any chance of automation. You’ll end up spending an entire afternoon manually drafting a Changelog instead of letting a machine do it in 3 seconds.

Conventional Commits: The Gold Standard for Pros

Instead of everyone writing their own style, the community agreed on a standard called Conventional Commits. It’s essentially a scientific naming convention that allows computers to understand and categorize your code history.

A standard commit looks like this:

feat(auth): add Google login feature

- Integrate Google OAuth2 SDK
- Update users table in database

Fixes: #123

Commit Types You Must Remember:

  • feat: A new feature (e.g., creating a checkout page).
  • fix: A bug fix (e.g., fixing price display on mobile).
  • refactor: Cleaning up code without changing functionality.
  • docs: Updating README, comments, or API documentation.
  • chore: Routine tasks like upgrading React libraries or configuring Docker.

This categorization is extremely powerful. You can quickly filter all new features from the past month with a simple grep command.

Upgrade: Let Tools Write Commits for You

If memorizing types is too hard, use Commitizen. Instead of typing manually, you’ll answer a few prompts to generate an accurate message.

Quick Install for Commitizen:

npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

Now, just type git cz. An interactive interface will appear; you just need to select the type of change and hit Enter.

Automating Changelogs: The “Lazy” Pro’s Secret

This is the most valuable part. When every commit follows the standard, you can generate a professional CHANGELOG.md with a single command:

npx standard-version

This tool automatically analyzes Git history to do three things: bump the version number (e.g., from 1.0.1 to 1.1.0), list changes in the Changelog file, and create a git tag. You’ll never have to manually type out change logs again.

Hard-Won Lessons from the Field

When I first introduced this standard to a team of 10, there was plenty of pushback. People complained it slowed down their coding speed. However, after two months, the loudest critics were the most grateful when they could track down old bugs in seconds.

A Few Tips for a Smoother Workflow:

  • Use Git Hooks: Install husky to block non-compliant commits. If someone types a message incorrectly, Git will reject the code, forcing them to fix it.
  • Leverage Scopes: Always specify the affected module, like feat(api) or fix(ui). This tells code reviewers exactly where to focus.
  • Quick Fixes: If you make a mistake, use git commit --amend immediately to fix the message instead of leaving a “stain” in your history.

Standardizing commit messages isn’t about being fancy. It’s the foundation for building professional CI/CD systems and liberating yourself from manual labor. Start today—a clean Git history is the mark of a conscientious developer!

Share: