我想在提交信息的结尾自动添加一个“Signed-off-by”行,以便为提交者签名。
当我设置
git config --global format.signoff true
并运行 git commit -m "modify something"
时,在 git log
中看不到“Signed-off-by”。如果我使用
git commit -m -s "modify something"
,则“Signed-off-by”将显示在 git log
中。有人能帮忙吗?
git config --global format.signoff true
并运行 git commit -m "modify something"
时,在 git log
中看不到“Signed-off-by”。git commit -m -s "modify something"
,则“Signed-off-by”将显示在 git log
中。git-interpret-trailers
命令自动签署任何未经过签署的提交。在即将发布的 Git 版本 2.15 中,该命令允许轻松检查现有的签署(无论其值/作者是什么)并添加您的签署(如果还没有)。截至2017年10月,所需的代码尚未包含在任何Git版本中(但在其master
分支中)!请将以下内容保存为 .git/hooks/prepare-commit-msg
或 .git/hooks/commit-msg
(有关差异,请参见此处),并使其可执行。#!/bin/sh
NAME=$(git config user.name)
EMAIL=$(git config user.email)
if [ -z "$NAME" ]; then
echo "empty git config user.name"
exit 1
fi
if [ -z "$EMAIL" ]; then
echo "empty git config user.email"
exit 1
fi
git interpret-trailers --if-exists doNothing --trailer \
"Signed-off-by: $NAME <$EMAIL>" \
--in-place "$1"
您可以通过创建一个文件(比如“~ /MYPROJECT/git-template”),其中包含一些空行和signed-off-by文本,来默认签署项目。
Signed-off-by: Your Name <your.email@example.com>
然后配置git使用该模板进行提交。例如:
git config commit.template ~/MYPROJECT/git-template
请确保您的项目文档明确记录了 "signed-off-by" 对于该项目的含义。以下是一些可以复制到 CONTRIBUTING.md 文件中的文本:
所有贡献(包括拉取请求)必须同意 Developer Certificate of Origin (DCO) 1.1 版本。这个版本与 Linux kernel 开发人员创建和使用的版本完全相同,并发布在http://developercertificate.org/上。这是开发人员的认证,表示他或她有权将补丁提交到项目中。简单地提交一个贡献即表示同意此协议,但请在每个补丁中包含一个“Signed-off-by”标记(这个标记是确认您同意 DCO 的常规方式)。
Git 2.14.x/2.15更新:正如我在“Git-检测提交是否已签名”中提到的,您将能够解析提交消息尾注以获取Signed-off-By
行。
"
git interpret-trailers
"已经学会了"--parse
"和其他一些选项,使脚本更容易从提交日志消息中获取现有的尾注行。
请参见stefanct的answer,了解commit-msg
客户端钩子如何使用git interpret-trailers
。
原始回答(2013)
format.signoff
是关于补丁(例如,在此补丁中所示):
format.signoff
一个布尔值,它让你默认启用
format-patch
的-s/--signoff选项。
这与git commit -s
无关。
换句话说,你不必在每个提交时都签名,但当你将它们作为补丁发布供他人使用(如"git: submitting patches")时,那么你应该签名。
关于Signed-of-by
的确切含义,请参见"What is the Sign Off feature in Git for?"。
--signoff/-s
选项,请按照以下步骤操作:cd <REPO>
cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg
edit .git/hooks/prepare-commit-msg
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
git-interpret-trailers
技术的解决方案。对我来说,决定性的优势是无论触发提交的工具及其配置如何,此方法都能正常工作--我经常使用git gui
进行提交。 - Dr. Jan-Philip Gehrckegit commit -n
进行类似操作),直接提交而不执行钩子。 - stefanct<your-repo>/.git/hooks/prepare-commit-msg.sample
中带有签名代码(已注释)。您需要执行cp prepare-commit-msg.sample prepare-commit-msg
,并取消注释以SOB=$(git var GIT_AUTHOR_IDENT...
开头的行和其后面的行。使用chmod +x <your-repo>/.git/hooks/prepare-commit-msg
使文件可执行,然后就可以开始了。 - vadasambar