0

I've changed my email address.

In GitHub, I now have "oldemail@gmail.com" and "newemail@gmail.com". 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 ?

  • See if this helps: https://www.reddit.com/r/git/comments/jp59k5/rebase_without_changing_commit_timestamps/ – Rafael Moreira Jan 11 '23 at 22:46
  • For this task, you are better off using a history rewriting tool such as [git-filter-repo](https://github.com/newren/git-filter-repo) instead of `git rebase` (whose actual purpose is quite different). They know how to keep timestamps and change only the property that you intend to change. That said, commit IDs will change regardless. – j6t Jan 12 '23 at 06:28

1 Answers1

1

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
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • Thank you, I will try this soon. I had done the "user.email" git command earlier btw. My current "user.name" is my Github username, should I necessarily change it ? – antoninhrlt Jan 12 '23 at 07:35
  • It seems to work, but only for the last commit. How to change all the commits ? – antoninhrlt Jan 12 '23 at 16:57
  • These steps worked for me for modifying a not-the-most-recent-commit. I used a little demo branch for it to test it and it definitely worked. Could you try with `-i HEAD~2` and change the not-most-recent commit and check? – tymtam Jan 13 '23 at 00:56
  • I tried with `-i HEAD~2` but the `commit` command only modify the most-recent-commit. How do you proceed exactly ? In my example, I would like to modify all the commits from a repo of mine. – antoninhrlt Jan 13 '23 at 18:21
  • In the interactive example I marked the commit I wanted to change as 'edit' – tymtam Jan 13 '23 at 22:50
  • I forgot to reply here, but it worked ! I've used the interactive mode and set "e" for each commit I wanted to change. Thank you ! – antoninhrlt Jan 22 '23 at 09:12