Skip to content

Instantly share code, notes, and snippets.

@shiryel
Last active February 23, 2022 23:04
Show Gist options
  • Save shiryel/d9f9b7b7e3c354b0a0ff24198cab6529 to your computer and use it in GitHub Desktop.
Save shiryel/d9f9b7b7e3c354b0a0ff24198cab6529 to your computer and use it in GitHub Desktop.
Git change email to allow signing correctly on old commits

Create an alias to simplify our lifes

git config --global alias.change-commits '!'"f() { VAR=\$1; OLD=\$2; NEW=\$3; shift 3; git filter-branch --env-filter \"if [[ \\\"\$\`echo \$VAR\`\\\" = '\$OLD' ]]; then export \$VAR='\$NEW'; fi\" \$@; }

Change the author and the committer emails on all commits

git change-commits GIT_AUTHOR_EMAIL "old@example.com" "new@example.com" -f -- --all
git change-commits GIT_COMMITTER_EMAIL "old@example.com" "new@example.com" -f -- --all

Without changing both, github would complain that the GPG signatures were invalid

Finally apply the GPG signatures on all commits

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

NOTE: to apply to a range of commits use: HEAD~4..HEAD instead of -- --all

To verify if everything is correctly use:

git log --show-signature --format=full

To sign future commits:

git config --global user.signingkey 5500BCA970D62D2D5614BCA242B23084B718647A
git config --global commit.gpgsign true

replace 5500BCA970D62D2D5614BCA242B23084B718647A by your own key id

If the github is giving the wrong datetimes use --committer-date-is-author-date on your rebase

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