Skip to content

Instantly share code, notes, and snippets.

@sbolel
Created June 21, 2023 16:48
Show Gist options
  • Save sbolel/589af0ec390d4c1460137958fc0fea12 to your computer and use it in GitHub Desktop.
Save sbolel/589af0ec390d4c1460137958fc0fea12 to your computer and use it in GitHub Desktop.
How to sign previous commits in a PR

To sign all your commits in the Git Pull Request (PR), you can use a combination of git rebase and git commit --amend. Here are the steps:

  1. Before starting, make sure you've configured Git to use your signing key. You can do this with:

    git config --global user.signingkey YOUR_SIGNING_KEY
    git config --global commit.gpgsign true

    Replace YOUR_SIGNING_KEY with your GPG key ID.

  2. Then you need to start an interactive rebase with the parent of your first commit. If you don't know what commit that is, you can use git log to display your commit history. Once you have your commit hash, start the rebase:

    git rebase -i YOUR_COMMIT_HASH^
  3. In the interactive rebase screen, change all the pick commands to edit for the commits you want to sign. Then save and exit the file (in vim, this is done with :wq).

  4. Git will now go through all the commits you marked as edit, and pause after each one. You can then amend and sign each commit like this:

    git commit --amend --no-edit --gpg-sign
  5. Once you're done with a commit, you can tell Git to continue to the next one with:

    git rebase --continue
  6. Git will then pause again for the next commit. Repeat the process until you've signed all the commits.

  7. If you already pushed your unsigned commits to the server, you'll have to force push your newly signed commits with:

    git push origin YOUR_BRANCH_NAME --force

    Replace YOUR_BRANCH_NAME with the name of your branch.

This should sign all the commits you've specified. Be aware that this will rewrite your Git history, which can cause problems for other people if they're also working on this branch. It's a good idea to let them know what you're doing so they can rebase their changes on top of your newly signed commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment