Amend author of Git commit and leave original commit date

Viewed 89

Is there a way to amend the author of Git commit and leave original commit date?

For example I changed a commit author on a commit that was pushed on Tuesday, however when I successfully changed the author, the commit date has also been changed to today, which is Wednesday. How do I change the author and keep the original date of Tuesday?

exec git commit --amend --author="John <john@mail.com>" -C HEAD
1 Answers

We can set the GIT_COMMITTER_DATE which is used for commit timestamp. By default it would be the current time. We can override it based on the commit timestamp by doing something like this:

exec GIT_COMMITTER_DATE="$(git show -s --format=%ci HEAD)" git commit --amend --author="John <john@mail.com>" -C HEAD

We can also update the authorDate by using git commit --amend --date based on commits timestamp.

exec git commit --amend --author="John <john@mail.com>" --date="$(git show -s --format=%ci HEAD)" -C HEAD

Related