Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signing an existing commit with GPG

Tags:

git

github

I'm looking for a way to replicate what git commit -S does but on a specific commit, by giving its SHA for instance.

Is it possible?

like image 822
chalasr Avatar asked Jun 09 '16 22:06

chalasr


People also ask

How do I get Git to sign all commits with gpg?

Use the git config user.signingkey option to specify the Key ID you just generated above for git to use. You can also require Git to sign all commits with the commit.gpgsign option. git config --global commit.gpgsign true git config --global user.signingkey "$ {MY_GPG_KEY}"

Why should I GPG sign my commits?

However, this can be avoided by simply GPG-signing your commits, one can prove that certain commits were originally done by you (and things that aren't signed shouldn't have made it into the production build). That's the key to it all, by signing commits you've added a stamp of approval - confirming that this is your work.

Does removing a GPG key unverify already signed commits?

Removing a key does not unverify already signed commits. Commits that were verified by using this key stay verified. Only unpushed commits stay unverified after you remove this key. To unverify already signed commits, you need to revoke the associated GPG key from your account.

How do I verify a GPG signature?

Within a project or merge request, navigate to the Commits tab. Signed commits show a badge containing either Verified or Unverified, depending on the verification status of the GPG signature. By clicking on the GPG badge, details of the signature are displayed. Revoking a key unverifies already signed commits.


2 Answers

Signing a commit will change the commit metadata, and thus change the underlying SHA1 commit ID. As you probably know, for Git, this has the same consequence of trying to change the contents of your history.

If you want to just re-sign your last commit you could run:

git commit -S --amend

If you want to re-sign a commit in the middle of your history you could do a couple of things, all of them being a bit nasty if you ask me:

  1. You could reset --soft to the commit you want to sign. Run git commit -S --amend and then commit all the staged changes. This would merge all your history after that commit into a single commit
  2. Branch out (for safety) and reset --hard to the commit you want to sign. Sign it, and if you want to perserve commit history you could now git cherry-pick NEXTCOMMIT -S to re-build the whole signed history.
like image 134
bitoiu Avatar answered Sep 26 '22 02:09

bitoiu


If you want to sign all the existing commits on the branch without do any changes to them:

git rebase --exec 'git commit --amend --no-edit -n -S' -i origin/HEAD
like image 40
Sergey Stadnik Avatar answered Sep 23 '22 02:09

Sergey Stadnik