Rewriting GIT history: how to replace the author of commits

Alexander Kropotin
3 min readNov 16, 2023

I work as a Java developer, and in my free time, I enjoy writing my pet projects for my personal GitHub profile. Whenever I have a spare moment or two, I always try to learn new technologies or attempt to implement them in my pet projects. Once, I wrote a new feature for one of my projects using my work laptop, which had the global user and email git settings configured; I forgot to set the local ones for my project. As a result, a new contributor appeared in the commit history of this project, using the user and email from my work account. I didn’t feel comfortable leaving a trace in the commit history of my personal GitHub profile with my work credentials, so I had to find a way to fix this.

Fortunately, two conditions were met for correcting the author data of the commits:

  1. In GIT, there is a solution — the `git filter-branch` command.
  2. My repository isn’t popular enough for a force push to be noticed by anyone.

Therefore, I rewrote the GIT history in this repository and changed the user and email data to my personal account. And I want to share this trick with you.

Attention: Before rewriting the GIT history in a work repository, please consult with your Team Lead or team, as these changes require a forced push of changes, which:

  • might be impossible due to branch restriction settings in the repository;
  • could cause conflicts for other people working with this repository.

Moreover, since something can always go wrong, it’s best to make a backup copy of the repository before starting to rewrite the GIT history.

To change the author data of commits in GIT, you need to:

1 — Make sure that the local repository contains all the changes from the remote; if necessary, execute the command:

git pull

2 — Execute the command to rewrite the history, having previously specified the required data: CURRENT_EMAIL — the email of the author of the commits to be changed in the history, NEW_NAME, and NEW_EMAIL — the data of the author of the commits for correction, respectively:

git filter-branch --env-filter '
CURRENT_EMAIL="current@e.mail"
NEW_NAME="new name"
NEW_EMAIL="new@e.mail"
if [ "$GIT_COMMITTER_EMAIL" = "$CURRENT_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$CURRENT_EMAIL" ]
then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

3 — Wait for the command to complete

4 — Force push the changes to the remote repository:

git push -f

If you follow these steps and everything goes correctly, then congratulations — the history will be rewritten.

--

--

Alexander Kropotin
Alexander Kropotin

Written by Alexander Kropotin

PhD in Physics & Mathematics with experience in software development and team management. Currently working as a Senior Software Engineer, specializing in Java

No responses yet