How to update git commit author, but keep original date when amending?

As mentioned in some other answers you would probably use:

git commit --amend --reset-author --no-edit --date="<old-date>"

While this works it's a lot of manual copying or typing to get the old date in place. You might want to get the date automatically, by getting only the date of the last entry in the log:

git log -n 1 --format=%aD

Combine the two and use some shell magic:

git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)"

This automatically sets the date of the last commit in the log, aka the one to be amended, as date of the new commit with the changed author.

Now changing the author on a larger amount of commits, say because you forgot to set the author in the cloned git repo, an interactive rebase is your friend:

git rebase -i <commit before wrong author and email>

You then change all commits you want to adjust from pick to edit and save the file. Git stops on every commit to be edited and you rerun:

git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)" && \
    git rebase --continue

If it's a reasonable small number of commit you can repeat this command using the shells arrow-up key until until the rebase finishes. If there is a larger number of commits, that typing arrow-up + return becomes too tedious you might want to create a small shell script that repeats the above command until the rebase finishes.


  1. If you are doing rebase then use committer-date-is-author-date to keep the date same as before.

    $ git commit --amend --committer-date-is-author-date
    
  2. For normal amend, copy the original committer time and override the time when amending using --date flag.

    $ git log                     # copy the 'original-committer-time'
    $ git commit --amend --reset-author --date="<original-committer-time>"
    
    # e.g. git commit --amend --date="Fri Dec 23 18:53:11 2016 +0600"
    

Tags:

Git