How to edit commit author without changing date?
Asked Answered
O

4

8

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?

Ovation answered 30/7, 2019 at 21:18 Comment(6)
git-scm.com/docs/git-filter-branch, particularly with --env-filterRodi
@Rodi I had find some questions where suggested this approach. But it's not helped me.Ovation
please provide an exact description of the problems you've faced with when you tried the filter-branch aprroach. I'm pretty sure it does help, because I did solve a similar problem a while ago. I could provide an exact solution if I knew all the circumstances of your task, particularly how many commits you need to rewrite, ow many branches and so on...Rodi
@Rodi 2 branches (master, develop), every commit from rootOvation
@Rodi ok, i found in git pro about filter-branch, and then this tutorial. So i resolve my main problem. Thx you. P.s. Could you tell me more about that --msg-filter, how do I point to the right commit for rewrite?Ovation
Does this answer your question? How to update git commit author, but keep original date when amending?Cephalonia
C
10

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>
Cephalonia answered 11/8, 2022 at 1:27 Comment(1)
The following (note the inclusion of name and email address) did amend Author and Commit fields but also updated the CommitDate field: git rebase --root --exec 'git commit --amend --author="My Name <[email protected]>" --no-edit' That can however be fixed with git rebase --committer-date-is-author-date --root. By contrast this command had exactly the desired effect of preserving all dates: git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE="%cD" GIT_AUTHOR_DATE="%aD" git commit --amend --no-edit --reset-author' rebase --root ```Damascene
D
4

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

Driftage answered 1/8, 2019 at 5:25 Comment(0)
R
3

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
Rouble answered 1/12, 2021 at 19:32 Comment(1)
This worked for me. I also needed to use the --force option for git filter-repo. After running the filter it removed my remote origin, so I had to add it back in git remote add origin ..... To push to my existing repository I had to use git push --force, or you can just create a new empty repository and push to that instead. All commits had the author properly updated and the commit dates were preserved. Thanks @tidreyer!Gothar
V
1

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

git rebase --ignore-date
Victualage answered 30/7, 2019 at 22:30 Comment(1)
i tried combine your suggesting command with my: $ git rebase --root --ignore-date --exec "git commit --amend --reset-author --no-edit" and got fatal: cannot combine am options with either interactive or merge optionsOvation

© 2022 - 2024 — McMap. All rights reserved.