关于公共密钥加密:是否可以使用GPG密钥在Git中“自动签名”提交?

Is there a way to “autosign” commits in Git with a GPG key?

有没有一种简单的方法可以使Git始终对创建的每个提交或标记进行签名?

我尝试了类似的东西:

1
alias commit = commit -S

但这并不能解决问题。

我不想安装其他程序来实现此目的。 容易做到吗?

只是一个附带的问题,也许不应该对提交进行签名,而应该只签上我从未创建过的标签,因为我为Homebrew等项目提交了单个提交。


注意:如果您不想一直添加-s来确保您的提交已签名,则有一个建议(目前为2013年12月的分支" pu",因此无法保证将其添加到git release)添加一个配置,它将为您处理该选项。
2014年5月更新:它在Git 2.0中(在此补丁系列中重新发送之后)

参见Nicolas Vigier(boklm)的commit 2af2ef3:

添加commit.gpgsign选项以签署所有提交

If you want to GPG sign all your commits, you have to add the -S option all the time.
The commit.gpgsign config option allows to sign all commits automatically.

1
commit.gpgsign

A boolean to specify whether all commits should be GPG signed.
Use of this option when doing operations such as rebase can result in a large number of commits being signed. It may be convenient to use an agent to avoid typing your GPG passphrase several times.

该配置通常是按每个存储库设置的(您无需签署您的私人实验本地存储库):

1
2
cd /path/to/repo/needing/gpg/signature
git config commit.gpgsign true

您可以将其与用作全局设置的user.signingKey结合使用(用于要签署提交的所有存储库的唯一密钥)

1
git config --global user.signingkey F2C7AB29

user.signingKey在git 1.5.0(2007年1月)中引入,提交为d67778e:

There shouldn't be a requirement that I use the same form of my name in
my git repository and my gpg key.
Further I might have multiple keys in my keyring, and might want to use one that doesn't match up with the address I use in commit messages.

This patch adds a configuration entry"user.signingKey" which, if present, will be passed to the"-u" switch for gpg, allowing the tag signing key to be overridden.

这是通过commit aba9119(git 1.5.3.2)强制执行的,以解决以下情况:如果用户在其.git/config中错误配置了user.signingKey或密钥环上没有任何秘密密钥。

笔记:

  • 按照惯例,自git 2.4.0自2015年3月起,它是signingKey而不是signingKey,即使git config键不区分大小写。仅当您执行git config --get-regexp(大小写敏感)时,这才有意义,否则,这只是可读性惯例;
  • 如果希望git服务器检查每次推送的签名,则至少需要git 2.2+(2014年10月)(提交b945901),因为git push --signed无法考虑user.signingKey配置值;
  • git 2.9(2016年6月)将使用user.signingKey强制对带注释的标签和提交进行签名:commit 61c2fe0。


1
2
git config --global user.signingKey 9E08524833CB3038FDE385C54C0AFCCFED5CDE14
git config --global commit.gpgSign true

将9E08524833CB3038FDE385C54C0AFCCFED5CDE14替换为您的密钥ID。 切记:使用短ID永远不是一个好主意。

更新:根据一个新的git命令,所有配置键都应位于camelCase中。


编辑:从Git版本1.7.9开始,可以签署Git提交(git commit -S)。稍微更新答案以反映这一点。

问题标题为:

Is there a way to"autosign" commits in Git with a GPG key?

简短的回答:是的,但是不要这样做。

解决以下问题中的错字:git commit -S不签署提交。而是从man git-commit页:

-s, --signoff
Add Signed-off-by line by the committer at the end of the commit log message.

这将提供类似于以下内容的日志输出:

1
2
3
4
5
6
7
8
± $ git log                                                                                 [0:43:31]
commit 155deeaef1896c63519320c7cbaf4691355143f5
Author: User Name
Date:   Mon Apr 16 00:43:27 2012 +0200

    Added .gitignore

    Signed-off-by: User Name

注意" Sign-off-by:..."位;是由git-commit上的-s标志生成的。

引用发布公告电子邮件:

  • "git commit" learned"-S" to GPG-sign the commit; this can be shown
    with the"--show-signature" option to"git log".

因此,可以签署提交。但是,我个人强烈建议您谨慎使用此选项;自动签署提交几乎没有意义,请参见下文:

Just a side question, maybe commits shouldn't be signed, only tags, which I never create, as I submit single commits.

没错提交未签名;标签是。原因可以在Linus Torvalds的此消息中找到,该消息的最后一段说:

Signing
each commit is totally stupid. It just means that you automate it, and you
make the signature worth less. It also doesn't add any real value, since
the way the git DAG-chain of SHA1's work, you only ever need one
signature to make all the commits reachable from that one be effectively
covered by that one. So signing each commit is simply missing the point.

我鼓励浏览链接的消息,这阐明了为什么自动签名提交不是一个比我可以做的更好的好主意。

但是,如果要自动对标签进行签名,则可以通过将git-tag -[s|u]包装在别名中来实现;如果要执行此操作,则可能要在~/.gitconfig或项目特定的.git/config文件中设置密钥ID。有关该过程的更多信息,请参见git社区书。签名标签比签署您的每次提交都更加有用。


要使自动签名在git版本2.0之前可用,您必须添加git别名进行提交。

1
2
3
# git config --global alias.commit commit -S
[alias]
    commit = commit -S

您需要明确指出,如果您签署了提交或标记,则并不意味着您同意整个历史记录。在提交的情况下,您只需在手头签名更改,在使用标签的情况下,..您需要定义它的含义。您可能已拉出一个更改,声称它是您发来的,但不是(因为其他人将其推到了您的遥控器上)。还是您不想进行的更改,但是您刚刚对标签进行了签名。

在典型的OSS项目中,这种情况可能不太常见,但是在企业场景中,您不时地仅触摸代码,而您并未阅读全部历史记录,这可能会被忽略。

如果将提交提交重新签名或与其他父母挑选,则签署提交是一个问题。但是,如果修改后的提交可以指向实际验证的"原始"提交,那将是很好的。