Is there a way to remove gpg sign of all previous commits or resign it with another gpg key
Asked Answered
W

2

5

I have just lost my old gpg key by accident. I'm wondering if I can remove each commit's gpg sign or resign it with my new gpg key?

Wanonah answered 2/7, 2020 at 16:38 Comment(4)
have you tried git rebase --exec 'git commit --amend --no-edit -S <keyid> ?Elbertelberta
Yeah, It gave me this error. error: pathspec 'xxxxxxxx' did not match any file(s) known to gitWanonah
You will need to replace each old commit with a new one: the new hash IDs will differ. @julian's command should work for this but note that it is -S<keyid> with no space; -S <keyid> is treated as -S, i.e., no key-ID specified, and then the keyid argument is treated as a file name (which gets you the pathspec error).Cedillo
Thank you for helping. It seems like the command only change one commit. Is there something like a batch process?Wanonah
S
5

I know this is an old question but i came across a similiar situation where i had to sign(re-sign actually) bunch of old commits. First confirm how many of the previous commits you want to (re)sign:

git log --show-signature

Suppose you want to sign the previous 5 commits then you can do:

git rebase -i HEAD~5

In the editor you will see your commits:

...
pick 4dd9ec5 fixed wrong config
pick 89d21f4 minor fix
...

Just add this line after every commit that you want to sign(re-sign).

exec git commit --amend --no-edit -s

So it would look like :

...
pick 4dd9ec5 fixed wrong config
exec git commit --amend --no-edit -s
pick 89d21f4 minor fix
exec git commit --amend --no-edit -s
...

Save and exit:

NOTE: Make sure you have force push priviledge (git push -f) in the upstream repository because this most likely would require to push with all force.

ANOTHER NOTE: With this method you can sign all but one previous commit. You may not be able to sign your initial commit.

Source

Swat answered 20/10, 2021 at 8:36 Comment(1)
Regarding your last comment: Doesn't git rebase --root allow that?Kenzie
C
2

To remove a GPG signature from a previous commit, first make sure commits are not signed automatically:

git config commit.gpgsign false

Then amend the commit with:

git commit --amend --no-edit --no-signoff

The --no-signoff option is the one that removes the signature

Cart answered 11/4, 2024 at 8:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.