Changing author of the pushed git commit in project
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
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:
- Open the git console in your repository location
- Pull the latest version
- Modify the script below to meet your requirements. (old_email, correct_name, correct_email)
git filter-branch -f --env-filter ' OLD_EMAIL="[email protected]" CORRECT_NAME="Jacob" CORRECT_EMAIL="[email protected]" 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
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 🙂