JonyStack
JonyStack

Reputation: 149

How to edit commit author without changing date?

I already know how to change the author of the commit (author and commit field),

git rebase --root --exec "git commit --amend --reset-author --no-edit"

but with the change of the author the dates (author date and commit date) are changed as of the current date. How do I save the old dates and change the author at the same time?

Upvotes: 8

Views: 3616

Answers (4)

DharmaTurtle
DharmaTurtle

Reputation: 8367

I answer this here. In short:

git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" GIT_AUTHOR_DATE="%aD" git commit --amend --no-edit --reset-author' rebase -f <commit/branch before wrong author and email, or --root to rebase all>

Upvotes: 12

tidreyer
tidreyer

Reputation: 31

None of the answers above worked properly for me here. Instead I used git filter-repo with the --mailmap option. After installing the git extension, follow these steps:

  1. Create a my_mailmap file like this:

    New Name <[email protected]> Old Name <[email protected]>
    
  2. Run the following command in your repos root directory:

    git filter-repo --mailmap my_mailmap
    

    (I additionally also needed the --force option)

Be warned though, this replaces the mail address and name in the entire history, make sure you know what you're doing. You can confirm the author and committer dates of a commit separately with git show -s --format=fuller:

Author:     Author Name <[email protected]>
AuthorDate: Wed Dec 30 10:27:44 2020 +0100
Commit:     Commiter Name <[email protected]>
CommitDate: Wed Dec 30 10:27:44 2020 +0100

Upvotes: 3

Jono Job
Jono Job

Reputation: 3028

This is not a complete solution to your question, as the commit date is still updated (this does change the commit after all), but it might be suitable for anyone that just wants to keep the author date unchanged.

Rather than using --reset-author with also updates the author date, you can just set the author explicitly.

git rebase --root --exec "git commit --amend --author=John --no-edit"

You can specify what you want as the author explicitly, or use a use a search pattern (which is what the example above does).

--author=

Override the commit author. Specify an explicit author using the standard A U Thor format. Otherwise is assumed to be a pattern and is used to search for an existing commit by that author (i.e. rev-list --all -i --author=); the commit author is then copied from the first such commit found.

Source

Upvotes: 4

CodeWizard
CodeWizard

Reputation: 142004

Use the --ignore-date flag or --committer-date-is-author-date

git rebase --ignore-date

Upvotes: 1

Related Questions