使用自定义模板保持 Git 提交消息一致
能够编写一致的 git 提交信息是一项技能,从长远来看,它对你和你合作的任何团队都有好处。如果你经常发现自己难以想出一个合适的信息格式,或者在回顾旧提交时无法理解自己之前的操作,这个小修复可能会有所帮助。而且最棒的是,它非常容易实现。
你可以通过 Git 的全局配置将一个文件设置为提交消息模板。我倾向于遵循约定俗成的提交格式,这样可以更轻松地设置消息格式,并使所有提交保持井然有序。Chris Beams 也写了一篇关于这个主题的精彩文章。
首先,在你选择的目录中创建一个 .txt 文件作为模板。我假设你使用终端进行此操作。
touch commit-conventions.txt
使用你选择的编辑器打开 .txt 文件。Git 默认会忽略以#
或;
开头的行,因此我们将使用这些行创建一个忽略行的模板,只有你在提交时才能看到。
注意:默认情况下,git 允许使用
#
和;
作为配置文件的注释字符,但您可以使用设置您选择的字符,并用您喜欢的字符git config --global core.commentChar '#'
替换。#
您可以在模板中放置任何您想要的内容,但这是我的一个示例:
# ----------------------------------------------------------
# Header - type(scope): Brief description
# ----------------------------------------------------------
# * feat A new feature - SemVar PATCH
# * fix A bug fix - SemVar MINOR
# * BREAKING CHANGE Breaking API change - SemVar MAJOR
# * docs Change to documentation only
# * style Change to style (whitespace, etc.)
# * refactor Change not related to a bug or feat
# * perf Change that affects performance
# * test Change that adds/modifies tests
# * build Change to build system
# * ci Change to CI pipeline/workflow
# * chore General tooling/config/min refactor
# ----------------------------------------------------------
# ----------------------------------------------------------
# Body - More description, if necessary
# ----------------------------------------------------------
# * Motivation behind changes, more detail into how
# functionality might be affected, etc.
# ----------------------------------------------------------
# ----------------------------------------------------------
# Footer - Associated issues, PRs, etc.
# ----------------------------------------------------------
# * Ex: Resolves Issue #207, see PR #15, etc.
# ----------------------------------------------------------
要将模板添加到全局 git 配置,请输入以下内容:
git config --global commit.template path/to/your/file.txt
现在,无论何时提交,都无需像往常一样输入git commit -m "A brief commit message"
,只需输入 Entergit commit
即可打开默认编辑器,其中包含模板。您将自动获得一个指南,可以从中选择相应的约定来创建结构化的消息。例如:
# ----------------------------------------------------------
# Header - type(scope): Brief description
# ----------------------------------------------------------
# * feat A new feature - SemVar PATCH
# * fix A bug fix - SemVar MINOR
# * BREAKING CHANGE Breaking API change - SemVar MAJOR
# * docs Change to documentation only
# * style Change to style (whitespace, etc.)
# * refactor Change not related to a bug or feat
# * perf Change that affects performance
# * test Change that adds/modifies tests
# * build Change to build system
# * ci Change to CI pipeline/workflow
# * chore General tooling/config/min refactor
# ----------------------------------------------------------
docs: Update README with contributing instructions
# ----------------------------------------------------------
# Body - More detailed description, if necessary
# ----------------------------------------------------------
# * Motivation behind changes, more detail into how
# functionality might be affected, etc.
# ----------------------------------------------------------
Adds a CONTRIBUTING.md with PR best practices, code style
guide, and code of conduct for contributors.
# ----------------------------------------------------------
# Footer - Associated issues, PRs, etc.
# ----------------------------------------------------------
# * Ex: Resolves Issue #207, see PR #15, etc.
# ----------------------------------------------------------
Closes #9
提交消息的“头部”标注了提交的类型docs
以及不超过 60 个字符的简短描述,以确保可读性(注释行长度为 60 个字符,用于指导何时使用换行符)。“主体”可选地详细说明所做的更改,“脚注”可选地注明与此提交相关的任何问题/PR。最终的消息将如下所示:
docs: Update README with contributing instructions
Adds a CONTRIBUTING.md with PR best practices, code style
guide, and code of conduct for contributors.
Closes #9
有关约定格式或它如何与语义版本控制配合使用的更多详细信息,请参阅我的git 配置。如果它对于您的用例而言过于混乱或过于复杂,请根据需要进行简化。
如果您使用 Vim 或 Neovim,并且想要进一步加快此过程,您可以将其添加到您的 git 配置中:
# Neovim
git config --global core.editor=nvim +16 -c 'startinsert'
# Vim
git config --global core.editor "vim +16 +startinsert"
这会将默认编辑器设置为 Neovim(或 Vim),并在编辑器打开后立即将光标置于插入模式的第 16 行。现在,无论何时提交,只要您输入git commit
,Neovim 就会打开模板,放置光标并立即接受输入。另请注意,您仍然可以git commit -m "Your message"
像之前一样使用 ,但当您输入 时,配置将默认为模板设置git commit
。
设置这个模板让我节省了无数的时间,避免了无数的挫败感,而且由于我所有的代码库都使用相同的全局提交格式,跟踪提交变得非常容易。你可以使用 Git 默认设置和复杂的 Githook 来发挥更多创意,但对于大多数开发者来说,这是一个解决常见问题的好方法。
如果您喜欢这个...
- ☕给我买杯咖啡来补充我的工作能量
- 在我的点文件中查看更多配置
- 查看我的作品集中完成的大型项目
- 在Twitter和LinkedIn上关注我。