mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
402 words
2 minutes
Say Goodbye to git push -f: A Git Commit Convention Guide for Team Collaboration

During team code reviews, the most headache-inducing thing is often not the code logic itself, but seeing a screen full of commit logs that look like:

  • update
  • fix bug
  • temp 123

This kind of “gibberish” turns historical backtracking into a detective game. Today, let’s talk about how to introduce Conventional Commits, making your Git history as clear as an instruction manual.

1. Why Do We Need Conventions?#

  1. Automated Changelog Generation: With a standard format, scripts can automatically grab commits starting with feat to generate release notes.
  2. Quickly Locate Issues: Seeing fix(user-auth) tells you instantly that it fixes a bug in the user login module.
  3. Trigger CI/CD: Certain types of commits (like docs) can be configured to skip time-consuming build processes.

2. Standard Format#

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

Structural Diagram#

Header: feat(auth): add google login support
│ │ │
│ │ └─ Subject (Short description)
│ └─ Scope (Impact area, optional)
└─ Type (Type: feature/fix/docs, etc.)
Body: (Empty line)
Specifically, used the OAuth2.0 SDK.
Added a new callback route.
Footer: (Empty line)
Closes #123 (Associated Issue)

2.1 Common Types#

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation changes
  • style: Formatting changes (doesn’t affect code execution, e.g., spaces, semicolons)
  • refactor: Refactoring (neither a new feature nor a bug fix)
  • perf: Performance optimization
  • test: Adding tests
  • chore: Changes to the build process or auxiliary tools

2.2 Examples#

feat(login): add google oauth login support

fix(nav): fix navbar overlap on mobile screen

3. Tool Assistance: Commitizen#

Don’t want to manually remember these rules every time? Use tools to enforce them.

Install commitizen:

npm install -g commitizen

Initialize the adapter:

commitizen init cz-conventional-changelog --save-dev --save-exact

When committing code in the future, don’t use git commit, but instead use:

git cz

It will pop up an interactive interface guiding you to select the type and fill in the description.

4. Forced Validation: Husky + Commitlint#

To prevent team members from “being lazy” and bypassing the conventions, you can intercept them in a Commit Hook.

  1. Install Husky and Commitlint.
  2. Configure the commit-msg hook.

When someone tries to submit an unstandardized message like git commit -m "update", Git will directly throw an error and reject the commit.

Common Errors and Solutions#

  1. Husky isn’t working?

    • Ensure you have executed npm install and the script triggered husky install.
    • In a CI environment, you might need to explicitly run npm run prepare.
  2. Garbage characters or line break issues on Windows

    • Using git cz on Windows might result in emojis displaying incorrectly. It is recommended to use Windows Terminal or Git Bash, and set UTF-8 encoding.
  3. Want to “escape” the convention?

    • If you are really in a rush to fix a production bug and don’t want to fill out complex forms, you can use the --no-verify parameter to skip the check:
      git commit -m "hotfix" --no-verify
    • Note: Use with caution!

5. Summary#

Git records are the historical archives of a project. Writing standardized Commit Messages is a sign of respect for your teammates and a reflection of your own professionalism.

Share

If this article helped you, please share it with others!

Say Goodbye to git push -f: A Git Commit Convention Guide for Team Collaboration
https://blog.levifree.com/posts/git-commit-convention/
Author
LeviFREE
Published at
2024-08-20
License
CC BY-NC-SA 4.0

Some information may be outdated

Table of Contents