Git代码提交,固定日志模板

Posted 编码小二的赵小二

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git代码提交,固定日志模板相关的知识,希望对你有一定的参考价值。

时间:2022年1月9日21:38:22

团队开发,但是每个人的日志风格不同该怎么办?

通过配置服务器的 Git 提交日志,就可以实现统一的代码提交风格。

先看实现效果,如下图

这样大家就必须按照现有的模板,填写对应内容,保持整体格式的统一。

Git 有提供一个示例文件,路径: .git/hooks/prepare-commit-msg.sample

查看文件内容,全是英文的,如下

zhaoc@ubuntu2004:~/09-GitRepository/TheCProgrammingLanguage/part-1/1-6$ cat ../../.git/hooks/prepare-commit-msg.sample
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source.  The hook's purpose is to edit the commit
# message file.  If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".

# This hook includes three examples. The first one removes the
# "# Please enter the commit message..." help message.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output.  It is
# commented because it doesn't cope with --amend or with squashed
# commits.
#
# The third example adds a Signed-off-by line to the message, that can
# still be edited.  This is rarely a good idea.

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"

# case "$COMMIT_SOURCE,$SHA1" in
#  ,|template,)
#    /usr/bin/perl -i.bak -pe '
#       print "\\n" . `git diff --cached --name-status -r`
#        if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
#  *) ;;
# esac

# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\\(.*>\\).*$/Signed-off-by: \\1/p')
# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
# if test -z "$COMMIT_SOURCE"
# then
#   /usr/bin/perl -i.bak -pe 'print "\\n" if !$first_line++' "$COMMIT_MSG_FILE"
# fi

文件最下边给出了两个示例,我们要用到的重点是这条语句

/usr/bin/perl -i.bak -pe 'print "\\n" if !$first_line++' "$COMMIT_MSG_FILE"

接下来修改自己的配置文件,复制原有的示例文件,命名为 prepare-commit-msg ,注意不带后缀!

cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg

修改 prepare-commit-msg 文件,直接屏蔽原代码语句,参考如下格式:

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

#/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"

case "$2,$3" in
	merge,)
		/usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
	,|template,)
		/usr/bin/perl -i.bak -pe 'print "修改描述:\\n影响分析:\\n是否自测:\\n自测内容:\\n测试建议:\\n代码审查:\\nNOTE:\\n" if /^#/ && $first++ == 0' "$1" ;;
	*) ;;
esac

修改完毕,先保存,再退出!

然后进行代码提交的测试即可,参考命令如下:

# 添加所有修改文件到缓存区(待提交区)
git add .

# 提交代码,并填写日志;
# 这一步会显示之前编写好的日志文件
git commit

# 填写完日志后,推送到服务器就可以啦
git push

好啦,我是小二,我们下期见~

以上是关于Git代码提交,固定日志模板的主要内容,如果未能解决你的问题,请参考以下文章

Git代码提交,固定日志模板

Git工程开发实践——Git工程实践扩展

Git学习01-提交代码,查看日志,回退,穿梭,撤销Github仓库代码的删除和拉取等

git提交代码日志规范

IDEA创建.gitignore文件模板忽略git提交.idea等文件

pip 意外没有安装带有分支/提交固定的最新版本的 git 包