Git 提交消息生成器
生成包含类型、范围和描述的Conventional Commits格式消息。
关于此工具
Git 提交消息生成器帮助开发者编写遵循 Conventional Commits 规范的清晰、标准化的提交消息。这种格式使版本控制日志易于阅读,支持自动版本控制和变更日志生成,并通过确保整个项目中的一致性来改进团队协作。无论是个人项目还是大型团队,格式正确的提交对于可维护性和可追溯性都至关重要。
使用该工具时,选择提交类型(feat、fix、docs、style、refactor、test、chore 或 perf),可选地添加范围(scope)以说明代码库的哪个部分发生了变化,编写简明的变更描述,并可选地包含更多详情或脚注信息(如问题参考)。该工具会立即生成格式正确的 Conventional Commit 消息,你可以直接复制并粘贴到版本控制系统中。
开发者、DevOps 工程师和开源维护者从采用 Conventional Commits 中获益最多,因为它支持语义版本控制自动化和专业的变更日志生成。虽然有些项目可能偏好自定义提交风格,但 Conventional Commits 格式得到 CI/CD 工具、GitHub Actions 和语义发布平台的广泛支持,是现代软件工作流的理想选择。
常见问题
代码实现
# Generate Conventional Commit messages programmatically
# No external library needed — plain string formatting
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class CommitMessage:
type: str # feat | fix | docs | style | refactor | test | chore ...
description: str
scope: Optional[str] = None
body: Optional[str] = None
breaking_change: Optional[str] = None
footer: list[str] = field(default_factory=list)
def render(self) -> str:
# Header: type(scope)!: description
scope_str = f"({self.scope})" if self.scope else ""
bang = "!" if self.breaking_change else ""
header = f"{self.type}{scope_str}{bang}: {self.description}"
parts = [header]
if self.body:
parts.append("")
parts.append(self.body)
trailers = list(self.footer)
if self.breaking_change:
trailers.append(f"BREAKING CHANGE: {self.breaking_change}")
if trailers:
parts.append("")
parts.extend(trailers)
return "\n".join(parts)
# Example usage
msg = CommitMessage(
type="feat",
scope="auth",
description="add OAuth2 login support",
body="Implements Google and GitHub OAuth2 providers.\nUpdates the session schema.",
breaking_change="remove legacy /login endpoint",
footer=["Reviewed-by: Alice <alice@example.com>"],
)
print(msg.render())
# feat(auth)!: add OAuth2 login support
#
# Implements Google and GitHub OAuth2 providers.
# Updates the session schema.
#
# Reviewed-by: Alice <alice@example.com>
# BREAKING CHANGE: remove legacy /login endpointComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.