git

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


If the commits are already made and pushed to the repository, and if want to change an author for some particular commit I can do it like this:

git commit --amend --reset-author

But, that will change the original committed date.

How can I reset author, but keep the original committer date?


Solution

  • Hm, at the end, I have found a bit easier solution for me. I have created a script in directory containing the git project gitrewrite.sh, and modified its permissions so it could be exectured:

    $ chmod 700 gitrewrite.sh
    

    Then I placed in the shell script:

    #!/bin/sh
    
    git filter-branch --env-filter '
    NEW_NAME="MyName"
    NEW_EMAIL="my-name@my-domain.com"
    if [ "$GIT_COMMIT" = "afdkjh1231jkh123hk1j23" ] || [ "$GIT_COMMIT" = "43hkjwldfpkmsdposdfpsdifn" ]
    then
        export GIT_COMMITTER_NAME="$NEW_NAME"
        export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
        export GIT_AUTHOR_NAME="$NEW_NAME"
        export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags
    

    And then run the script in terminal:

    $ ./gitrewrite.sh
    

    And that's it. The history has been rewritten.

    Push the code to the repository and add a force flag.

    $ git push -f

    Important note:

    For the others reading it, keep in mind that this is going to create new references in the git history so do this only in private repository or the one which is still not shared with others as it could cause broken references!

    Mine was a private repo, so no worries there for that part. If it is public, then perhaps using the other suggested answer could be a better option.