简体   繁体   English

有没有办法对所有以前的提交进行 gpg 签名?

[英]Is there a way to gpg sign all previous commits?

As the title says, I'm looking for a way to gpg sign all my previous commits in a repository (preferably without typing in my passcode for every commit).正如标题所说,我正在寻找一种方法来对存储库中所有以前的提交进行 gpg 签名(最好不要为每次提交输入密码)。

Thanks!谢谢!

You can, but it will have to rewrite your entire history to do so.你可以,但它必须重写你的整个历史才能这样做。

Signing a commit changes the commit which changes its commit ID.签署提交会更改提交,从而更改其提交 ID。 Since the commit ID depends on the previous commit ID, all commits after that have to be changed.由于提交 ID 取决于先前的提交 ID,因此必须更改此后的所有提交。 And you're signing them all anyway.无论如何,你都在签署他们。

If it's a personal repository that nobody else is working on, then it's not a problem.如果它是一个没有其他人在工作的个人存储库,那么这不是问题。 If it's a repository with other collaborators, treat it like doing a major rebase.如果它是与其他协作者的存储库,请将其视为进行重大变基。

You'd do it with git filter-branch to redo every commit with the -S option.您可以使用git filter-branch来使用-S选项重做每次提交。

git filter-branch --commit-filter 'git commit-tree -S "$@";' -- --all

As for not having to type in your passcode for every commit, you need to configure gpg to use a gpg-agent .至于不必为每次提交输入密码,您需要配置gpg以使用gpg-agent If you're familiar with ssh-agent it's a similar idea, it's a little process that you give the password to once and keeps it stored in memory for you.如果您熟悉ssh-agent这是一个类似的想法,这是一个小过程,您将密码提供一次并将其保存在内存中。 How you do that depends on your operating system and setup.您如何做到这一点取决于您的操作系统和设置。 On OS XI let GPG Tools take care of it.在 OS XI 上,让GPG 工具来处理它。

My approach is我的方法是

git rebase --exec 'git commit --amend --no-edit -n -S' -i 8fd7b22

All commits started from the next after 8fd7b22 will be rebased with no changes except signing.8fd7b22之后的下一个开始的所有提交8fd7b22将重新定位,除了签名之外没有任何更改。 To change all commits started from the very first one you may use --root (since Git v1.7.12 ):要更改从第一个提交开始的所有提交,您可以使用--root (自Git v1.7.12 起):

 git rebase --exec 'git commit --amend --no-edit -n -S' -i --root

To spread changes to the remote I use将更改传播到我使用的遥控器

git push --force

Note, this will update "gpg made" date-time and, for example, GitHub will treat it as commit date.请注意,这将更新“gpg made”日期时间,例如,GitHub 会将其视为提交日期。 Git itself persists both original and new dates, git log --show-signature gives clear picture of when the original commit was made and when it was signed for the last time. Git 本身保留原始日期和新日期, git log --show-signature清楚地说明原始提交的时间以及最后一次签名的时​​间。

If you want to filter only specific commits and sign only them you can use filter-branch :如果您只想过滤特定的提交并仅对它们进行签名,您可以使用filter-branch

git filter-branch --commit-filter 'if [ "$GIT_COMMITTER_EMAIL" = "user@domain.com" ];
  then git commit-tree -S "$@";
  else git commit-tree "$@";
  fi' HEAD

This is useful if, for some reason, you want to sign only your own commits.如果出于某种原因,您只想签署自己的提交,这将非常有用。

The use-case I wanted to solve is to sign all of my previous commits while keeping the original commits dates.我想要解决的用例是在保留原始提交日期的同时签署我以前的所有提交。

Sign relevant commits签署相关提交

git rebase -i --root
git commit --amend -S --no-edit &&  git rebase --continue # for all the relevant commits

Return commit date as author date and force push (don't forget to backup before).将提交日期返回为作者日期并强制推送(之前不要忘记备份)。

git rebase --committer-date-is-author-date -i --root # return 
git push origin main -f

Can be automated using the other answers.可以使用其他答案自动化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM