git 提交代码的时候, 添加自己名字

自定义提交信息, 协同开发人员太多的时候, 从提交信息里就能知道谁的代码

进入自己项目, 打开.git 文件, 找到hooks 文件夹, 新建一个文件, 命名 prepare-commit-msg , 如果不能这样的命名, 就先随便命名一个, 后续再改, 然后把以下文件内容复制出来, 
#!/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
sed -i '' "1s/^/自己的名字| /" $COMMIT_MSG_FILE


/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

这是全部的文件代码, 文件名 改为prepare-commit-msg

image.png

至此, 完成了第1步
要想实现 2 的样子
接下来要让这个文件在提交代码的时候,起作用
进入项目, 执行cd .git/hooks指令
然后执行 chmod +x prepare-commit-msg指令 没有权限的话, 执行 sudo添加权限
这样, 文件类型, 就会变成可执行文件类型,
打开自己的sourcetree , 就能看到自己的名字的提交记录了

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容