繁体   English   中英

如何强制“git commit -m”打开编辑器

[英]How to force "git commit -m" to open editor

我正在为团队实施 git 模板提交消息。 我已经通过.git/hooks/prepare-commit-msg钩子通过添加一行来完成它:

cat ".gitmessage" >> "$1"

我还配置了以下内容:

git config --local commit.template .gitmessage

嗯,模板功能工作正常,但只有在没有-m标志的情况下调用 git commit 时。

不幸的是,所有团队成员的工作流程是:

git add ...
git commit -m "Some message"

问题:如何强制 git 始终打开编辑器来编辑消息,即使被git commit -m ...命令调用时也是如此?

-e 打开编辑器。

git commit -m "message" -e

您可以修改commit-msg预挂钩来实现此目的。 例如,您可以检查提交消息中的行数(检查下面的示例); 使用一些正则表达式来检查模板是否被遵守; 等等。

#!/bin/bash

# Check if the number of lines is greater than 3
commit_msg_file="${1}"
minimum_lines=3
num_lines=$(wc -l "${commit_msg_file}" | cut -f1 -d' ')

if (( $num_lines < $minimum_lines )); then
    echo >&2 "Error!"
    exit 1
fi

检查thisthis以供参考。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM