11

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?

chalasr
  • 12,503
  • 3
  • 38
  • 79
  • Possible duplicate of https://superuser.com/questions/397149/can-you-gpg-sign-old-commits, but it is not recommended https://superuser.com/questions/1144817/is-it-a-good-idea-to-gpg-sign-old-git-commits – Michael Freidgeim Nov 25 '20 at 14:14
  • Does this answer your question? [Is there a way to gpg sign all previous commits?](https://stackoverflow.com/questions/41882919/is-there-a-way-to-gpg-sign-all-previous-commits) – Michael Freidgeim May 05 '22 at 21:18
  • Yes, slightly different but it works. My question is older than the two questions you mentioned though, I voted to close the other. – chalasr May 05 '22 at 23:04
  • "Possible duplicate" is a way to clean-up - to close similar questions and keep one with the **best answers**. The date is not essential. See http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha If you agree that it requires clarification please vote on http://meta.stackexchange.com/questions/281980/add-clarification-link-to-possible-duplicate-automated-comment – Michael Freidgeim May 06 '22 at 13:50

2 Answers2

10

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.
bitoiu
  • 6,204
  • 5
  • 37
  • 57
  • I was using `--amend -S` for my last commit but I wasn't able to do the same back in the history. Great, thank you @bitoiu – chalasr Jun 10 '16 at 08:24
6

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
Sergey Stadnik
  • 2,980
  • 7
  • 26
  • 29