Update git commit email address for a commit in the past

Viewed 100

I was working on a branch and made several commits. git log looks like this:

commit drginrd45345eff34wfe... (HEAD -> peterdoe/my-new-branch)
Author: Peter Doe <peterdoe@xyzcom>
Date:   Fri Jul 6 19:10:12 2021 +0200

    update3

commit rey4tgrege45ge343rg...
Author: Peter Doe <peterdoe@peterdoe.dev.xyz.com>
Date:   Thu Jul 5 18:12:43 2021 +0200

    update2

commit 6745trge4te4t4t4t4g...
Author: Peter Doe <peterdoe@xyz.com>
Date:   Thu Jul 5 11:21:12 2021 +0200

    update1

I can not push my branch to open merge request as the author email on update2 is not the same as the other two commits. How can I update the author email for commit rey4tgrege45ge343rg to also be peterdoe@xyz.com, while changing nothing else so I can open my MR with all three commits?

1 Answers

If the changes are on your own branch, you can safely re-write history to change the commit author.

In your case you can start an interactive rebase starting with the commit you want to change from the terminal with

git rebase -i -p rey4tgr

You will be able to mark the commits you want to edit and then git will walk you through each commit asking you to make your changes. For changing the author email you can use

git commit --amend --author="Peter Doe <peterdoe@xyz.com>" --no-edit
git rebase --continue

And do this until you have made all the changes you want.

Related