antoninhrlt
antoninhrlt

Reputation: 43

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 ?

Upvotes: 1

Views: 2268

Answers (2)

Ehsanullah Qadeer
Ehsanullah Qadeer

Reputation: 1

To update the author name and email for all previous commits in a Git repository, you can use the following script:

git filter-branch --env-filter '
NEW_NAME="Your Name"
NEW_EMAIL="[email protected]"

GIT_COMMITTER_NAME="$NEW_NAME"
GIT_COMMITTER_EMAIL="$NEW_EMAIL"
GIT_AUTHOR_NAME="$NEW_NAME"
GIT_AUTHOR_EMAIL="$NEW_EMAIL"

export GIT_COMMITTER_NAME
export GIT_COMMITTER_EMAIL
export GIT_AUTHOR_NAME
export GIT_AUTHOR_EMAIL
' --tag-name-filter cat -- --all

Steps:

  1. Replace "Your Name" and "[email protected]" with your desired name and email.
  2. Run the command to rewrite the commit history.
  3. After updating the commits locally, push the changes to the remote repository with:
git push origin --force --all
git push origin --force --tags

Important Note

Backup First: This command rewrites history, so make sure to back up your repository beforehand .

Upvotes: 0

tmaj
tmaj

Reputation: 34987

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

Upvotes: 2

Related Questions