git commit 输入 message 方式和规范

方式

常见的-m <msg>单行输入

git commit -m "message" .

当要输入多行时,可以不带参数,直接使用git commit会启动默认文本编辑器(vi/vim),自动打开.git/COMMIT_EDITMSG文件 在其中输入内容保存退出即可。

git commit

重用其它commit的message

-C <commit>, --reuse-message=<commit>
    Take an existing commit object, and reuse the log message and the
    authorship information (including the timestamp) when creating the
    commit.

-c <commit>, --reedit-message=<commit>
    Like -C, but with -c the editor is invoked, so that the user can
    further edit the commit message.

--reset-author
    When used with -C/-c/--amend options, or when committing after a
    conflicting cherry-pick, declare that the authorship of the resulting
    commit now belongs to the committer. This also renews the author
    timestamp.

使用-C参数不能重新编辑提交信息,相当于-c <commit> --no-edit,其会完全重用提交信息,包括作者信息和提交时间截 可以添加--reset-author参数使用当前的作者信息和提交时间

git commit -C HEAD --reset-author

从文件或标准输入中读取message信息

-F <file>, --file=<file>
    Take the commit message from the given file. Use - to read the
    message from the standard input.

.git/COMMIT_EDITMSG文件中#开头行是注释行,不会提交到message中;但是-F参数会包含#开头行, 可以添加--amend参数,则会将#开头行视为注释行去掉。

git commit --amend -F <file>

使用-来表示读取标准输入内容

echo "message" | git commit -F -

在需要固定message格式的场景中,可以使用模板方式-t <file>提交

-t <file>, --template=<file>
    When editing the commit message, start the editor with the contents
    in the given file. The commit.template configuration variable is
    often used to give this option implicitly to the command. This
    mechanism can be used by projects that want to guide participants
    with some hints on what to write in the message in what order. If the
    user exits the editor without editing the message, the commit is
    aborted. This has no effect when a message is given by other means,
    e.g. with the -m or -F options.

模板方式会启动文本编辑器,需要进行改动,如果没有改动提交会中止 可以用git config设置参数commit.tmplate指定默认的template file

不写提交信息方式

 --allow-empty
     Usually recording a commit that has the exact same tree as its sole
     parent commit is a mistake, and the command prevents you from making
     such a commit. This option bypasses the safety, and is primarily for
     use by foreign SCM interface scripts.

 --allow-empty-message
     Like --allow-empty this command is primarily for use by foreign SCM
     interface scripts. It allows you to create a commit with an empty
     commit message without using plumbing commands like git-commit-
     tree(1).

规范

常用提交格式是Angular团队的规范,格式如下:

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

主要分为三部分(Header,Body, Footer):

标题行(Header): 必填, 描述主要修改类型和内容;type(必需)、scope(可选)和subject(必需)

type用于说明 commit 的类别

feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style: 格式(不影响代码运行的变动)
refactor:重构(即不是新增功能,也不是修改bug的代码变动)
test:增加测试
chore:构建过程或辅助工具的变动

建议type为(feat和fix)的 commit 出现在 Change log 之中。 其他(docs、chore、style、refactor、test)不放入Change log中。

scope用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

subject是 commit 目的的简短描述,不超过50个字符。

以动词开头,使用第一人称现在时,比如change,而不是changed或changes
第一个字母小写
结尾不加句号(.)

主题内容(Body): 描述为什么修改, 做了什么样的修改, 以及开发的思路等等

Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例

More detailed explanatory text, if necessary.  Wrap it to about 72 characters or so.

Further paragraphs come after blank lines.

- Bullet points are okay, too
- Use a hanging indent
  • 建议使用第一人称现在时,比如使用change而不是changed或changes。

  • 应该说明代码变动的动机,以及与以前行为的对比。

页脚注释(Footer): 放 Breaking Changes 或 Closed Issues

不兼容变动(Breaking Changes)

如果当前代码与上一个版本不兼容,则 Footer 部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法。

BREAKING CHANGE: isolate scope bindings definition has changed.
    To migrate the code follow the example below:
    Before:
    scope: {
      myAttr: 'attribute',
    }
    After:
    scope: {
      myAttr: '@',
    }
    The removed `inject` wasn't generaly useful for directives so there should be no code using it.

关闭 Issue(Closed Issues)

如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue

Closes #123

也可以一次关闭多个 issue

Closes #123 #234

git commit 模板

修改 ~/.gitconfig, 添加:

[commit]
template = ~/.gitmessage

新建 ~/.gitmessage 内容可以如下:

# head: <type>(<scope>): <subject>
# - type: feat, fix, docs, style, refactor, test, chore
# - scope: can be empty (eg. if the change is a global or difficult to assign to a single component)
# - subject: start with verb (such as 'change'), 50-character line
#
# body: 72-character wrapped. This should answer:
# * Why was this change necessary?
# * How does it address the problem?
# * Are there any side effects?
#
# footer: 
# - Include a link to the ticket, if any.
# - BREAKING CHANGE
#

参考