提交信息失落的艺术
The Lost Art of Commit Messages

原始链接: https://www.seyhan.me/blog/post/lost-art-of-commit-messages

提交信息是开发过程中至关重要,却常被忽视的一部分。不要使用模糊的、单字描述,而应力求清晰明了地解释代码修改背后的原因。 遵循以下格式:`(可选 scope): <简短描述>`。类型包括 `feat`、`fix`、`chore`、`refactor`、`docs`、`style`、`test`、`perf`、`ci`、`build` 和 `revert`。scope 指定受影响的区域(例如,`auth`、`ui`)。描述应简洁(少于 50 个字符)并以小写字母开头。 可以选择添加项目符号以提供详细的上下文,并在页脚添加元数据。页脚可以包含 `BREAKING CHANGE`、`Fixes`、`Closes`、`Resolves`、`Related to`、`Co-authored-by`、`Reviewed-by`、`Signed-off-by` 和 `See also`。 优先考虑清晰度、一致性和相关性。使用现在时态,避免冗余。周全的提交信息可以改善协作、调试和项目理解,避免你和团队成员头疼。不要只是“修复问题”;解释你如何以及为什么修复它!

这篇 Hacker News 帖子讨论了提交信息的值和风格,起因是一篇文章提倡使用描述性的提交信息而不是严格格式化的提交信息。原作者认为像 "chore:" 这样的前缀不如用这些字符来添加细节更有用,尤其是在二分查找 bug 时。他们鼓励个人的写作风格和幽默感。 评论者们就理想的内容展开了辩论。一些人更喜欢简洁的、总结变更的提交信息,这可以通过分支合并(branch-and-squash)来实现。另一些人,例如 kqr 和 nloomans,批评了这些简短的总结,强调了解释变更 *原因* 的重要性,包括代码背后的需求和上下文。他们认为像“修复 X”或“添加 Y”这样的模糊信息在追踪 bug 时毫无帮助。一些人建议加入“原因”以防止未来的误解或破坏性更改。 其他观点包括 Gerrit 在审查提交信息方面的有用性,使用 Cursor 规则编写提交信息的可能性,以及对特定格式的偏好(简短描述,空行,详细信息)。一些人认为冗长的提交信息是浪费,而另一些人则认为它们对于调试和理解代码历史至关重要。讨论涉及到常规提交标准以及提交信息的用途,这些用途超出了变基(rebase)。
相关文章
  • (评论) 2023-12-27
  • (评论) 2024-02-02
  • 我最喜欢的 Git 提交 (2019) 2024-02-02
  • (评论) 2024-08-08
  • (评论) 2023-12-20

  • 原文

    Have you ever noticed how commit messages these days are like cryptic haikus from developers who seem to think a single word is all it takes to summarize their masterpiece?

    People scribbling "fix bug", "update code", or the ever descriptive "refactor" while pushing changes that could rival a novel in length. It's as if they think we're all mind readers or perhaps they assume their code is so flawless it needs no explanation.

    For the love of clean code history, let's remember we're not monkeys banging on keyboards; we're educated, civilized human beings.

    The Art of the Commit Message

    Crafting a meaningful commit message seems to be a lost art in today's fast-paced development world.

    Yes! I said "lost art". Because many developers or let me correct that, many keyboard virtuosos apparently don't see the value in leaving a traceable breadcrumb trail of their genius.

    Okay, let's learn how to do it right.

    Commit messages are the narrative of your project's history. They help others (and your future self) understand why changes were made, making collaboration smoother and debugging less of a nightmare.

    Given that, let's dive into some guidelines to improve your commit messages from vague to valuable.

    Commit Message Guidelines

    Each commit message should follow this structure:

    <type>(optional scope): <short description>
    <BLANK LINE>
    - Optional point 1 in brief
    - Optional point 2 in brief
    <BLANK LINE>
    <footer>
    

    Components of the Format

    1. Type: Specifies the nature of the commit.
    2. Scope (optional): A short identifier for the component, file, or feature affected.
    3. Short Description: A concise summary of the change, ideally up to 50 characters.
    4. Detailed Points (optional): Bullet points that provide additional context or details about the changes.
    5. Footer (optional): Additional metadata like issue references, co-authors, or breaking changes.

    Guidelines for Each Component

    Type

    • Required: Select the appropriate type based on the commit's purpose.
    • Use only one of the allowed types.

    Allowed Types

    Choose from the following types for the <type> field:

    • feat: A new feature
    • fix: A bug fix
    • chore: Routine tasks that don't impact functionality
    • refactor: Code changes that neither fix bugs nor add features
    • docs: Documentation updates
    • style: Code style changes that don't affect functionality
    • test: Adding or updating tests
    • perf: Performance improvements
    • ci: Continuous Integration changes
    • build: Changes related to build processes or dependencies
    • revert: Reverts a previous commit

    Scope (Optional)

    • Optional: Specifies the part of the codebase affected.
    • Keep it short and descriptive.
    • Use lowercase and single-word terms (hyphens are acceptable).

    Examples:

    • auth, api, ui, database, config.

    Short Description

    • Required: Summarize the change in 50 characters or less.
    • Start with a lowercase letter; no punctuation at the end.
    • Clearly convey the essence of the change.

    Examples:

    • add user authentication
    • fix null pointer exception in payment module
    • refactor data processing pipeline

    Detailed Points (Optional)

    • Optional: Provide additional context in bullet points.
    • Keep each point brief and focused.
    • Use bullets (-) and avoid lengthy explanations.

    Examples:

    • - implement OAuth2 for third-party login
    • - adjust timeout settings to prevent crashes
    • - reorganize folder structure for clarity

    Footer (Optional)

    You might wonder, "Footers? Isn't that a bit much?" Well, consider this: footers can automate tedious tasks, improve team communication, and make your commit history a beacon of clarity in the chaotic sea of code changes.

    Now, the optional footer section is where you can sprinkle in extra metadata that helps with automation, issue tracking, and keeping your project history as pristine as a freshly refactored codebase.

    Footers are additional lines at the end of your commit message that provide metadata about the commit. They can:

    1. Automate issue tracking: Automatically close or reference issues and pull requests.
    2. Provide additional context: Include information like co-authors, sign-offs, or breaking changes.
    3. Enhance clarity: Make your commit history more informative and navigable.

    Common Footer Tags

    Because why settle for clarity when you can have extra clarity? Here are some standard footer tags you can use:

    • BREAKING CHANGE: Signals that the commit introduces a backward-incompatible change.
    • Fixes: References an issue your commit fixes.
    • Closes: Similar to "Fixes", but used to close issues or pull requests.
    • Resolves: Another way to reference and close issues.
    • Related to: Links to issues or tasks related to the commit without closing them.
    • Co-authored-by: Credits additional contributors.
    • Reviewed-by: Indicates who reviewed the changes.
    • Signed-off-by: A sign-off acknowledging you have the rights to submit the work.
    • See also: Points to additional resources or relevant information.

    Example Commit Messages

    Example 1: Feature Addition

    feat(auth): add user login functionality
    
    - implement JWT authentication
    - add login endpoint to API
    

    Example 2: Bug Fix that Closes an Issue

    fix(ui): correct alignment on dashboard widgets
    
    - adjust CSS flex properties
    - test on multiple screen sizes
    
    Fixes #204
    

    Example 3: Documentation Update

    docs(readme): update installation instructions
    
    - add steps for setting up the development environment
    - include troubleshooting tips
    

    Example 4: Code Refactor

    refactor(api): simplify request handlers
    
    - remove redundant code blocks
    - improve error handling mechanisms
    

    Example 5: Documentation Update with See Also

    docs(README): update setup instructions
    
    - include Docker configuration
    - clarify environment variables
    
    See also: https://example.com/setup-guide
    

    General Tips

    • Be concise and clear: Your future self (and teammates) will thank you.
    • Avoid redundancy: Don't repeat the same information.
    • Use present tense: "Add feature", not "Added feature".
    • Be Consistent: Use standard tags to keep the commit history uniform.
    • Keep It Relevant: Only include footers that add value or necessary context.
    • Stay Professional: This isn't the place for jokes or unnecessary commentary (that's what the code comments are for).

    Next time you're tempted to type "fix stuff" as a commit message, remember, we're not monkeys. We're educated, civilized human beings capable of conveying complex ideas with clarity. Let's make sure our commit messages reflect that if not for the sake of our teammates, then at least to prevent our future selves from cursing our past selves.

    Happy committing!

    联系我们 contact @ memedata.com