我是靠谱客的博主 笨笨鸭子,最近开发中收集的这篇文章主要介绍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代码提交,固定日志模板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部