Changing author of the pushed git commit in opensource project


Blog

Sometimes we have to change author of the pushed commit. How to do it without much efford?

Today I stuck on similar problem. I was using SourceTree to commit some changes to be available on my second computer… Unfortunately I didn’t realise which user is associated with it. Just after push saw wrong author next to the commit. Because I’m not very familiar with console git, quick google gave me some ideas how can I fix it.

It took me about 2 hours to try some examples and get the things works but I found a nice way that is presented below and at
https://help.github.com/articles/changing-author-info/

CAUTION! As @Marcin wrote in comments bellow you have to be careful doing this operation. It change commit hash and can lead to problems when you cooperate with other people!

How to change author of  ALL wrong pushed commits:

  1. Open the git console in your repository location
  2. Pull the latest version
  3. Modify the script below to meet your requirements. (old_email, correct_name, correct_email)
    git filter-branch -f --env-filter '
    OLD_EMAIL="someWrongEmail@private.com"
    CORRECT_NAME="Jacob"
    CORRECT_EMAIL="jacob@correct.com"
    if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
    then
     export GIT_COMMITTER_NAME="$CORRECT_NAME"
     export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
    then
     export GIT_AUTHOR_NAME="$CORRECT_NAME"
     export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags
    
  4. After modyfing it, paste it to console and press ENTER
  5. Verify correctness by looking at the SourceTree gui or by typing
     git log
  6. When all changes are correct, run:
     git push --force --tags origin 'refs/heads/*' 

Now all commits with the old author should be replaced and fixed 🙂


As always: subscribe to my RSS feed!
follow us in feedly


  • Be aware that by changing commit author you are changing commit hash, in other words if someone else pulled your project he/she will have terrible merge conflicts because git will think that you pulled from different repo. In git it is really a bad idea (TM) to change already pushed commit when you are working with other persons.

    • Jakub Boczek

      @0xmarcin:disqus Thanks for the good remark 🙂 I’ll put a note at the beginning of the article. I think that in specific situation it can be done but we just have to be careful.