Search code examples
gitgithubversion-controltimestamprebase

Change the git commit author's email but keep the same commit timestamps


I've changed my email address.

In GitHub, I now have "[email protected]" and "[email protected]". So, with whatever email I use to commit and push on GitHub, these commits are recognised as mine.

Note that for the following explanations, I'm using a repository created for the test. I didn't want to destroy my important repositories.

But I would like to change all my commits to using my new email, to be able to remove my old email from Github and be no longer associated with.

I've seen this question : How do I change the author and committer name/email for multiple commits?

The highest scored answer propose that :

git rebase -r <some commit before all of your bad commits> \
    --exec 'git commit --amend --no-edit --reset-author'

I've done that for a repository, to change all the commits from the first one :

git rebase -r --root \
    --exec 'git commit --amend --no-edit --reset-author'

It works, but when I push it onto Github, my commit is published with timestamp = now. But actually, my commit has been done earlier. Its timestamp has been changed.

commit timestamp as 'now'

I found something about git and timestamps : git rebase without changing commit timestamps

So I tried again for a fresh test repository :

git rebase -r --root --committer-date-is-author-date \
    --exec 'git commit --amend --no-edit --reset-author'

But it changed nothing in apparence, I got the same problem : the timestamp = now even if the commit was done earlier.

-> How to change both the commit author's email, and keep the original commit timestamps ?


Solution

  • In summary, --committer-date-is-author-date should work.

    This is because GitHub shows committer name and date, not author name and date (in some places it shows both). You can see the committer data with git log --pretty=fuller.

    To change the committer and author and keep the dates as original this works

    1. Change your email git config user.email 'xxx' - this is so that the committer - not just the author - is what you want.
    2. Change your name git config user.name 'xxx'
    3. Rebase git rebase -i HEAD~2
    4. git commit --amend --no-edit --reset-author --date="$(git log -n 1 --format=%aD)"
      or
      git commit --amend --no-edit --author="TODO" --date="$(git log -n 1 --format=%aD)"
    5. Rebase --committer-date-is-author-date, for example git rebase -i HEAD~2 --committer-date-is-author-date to update the committer dates