DEV Community

Cover image for Replace accidentally committed git author information from git history

Replace accidentally committed git author information from git history

Deshan Madurajith
Software Engineer. Help small tech startups
Updated on ・2 min read

If you working for multiple projects or companies, you may use different Github logins. If you accidentally commit something as wrong git user, it will be a pain to change the history. If you push it and add more commits on that it will be more painful to revert those changes.

If you search on google, you will see a lot of solutions. Most solutions are not straight forward, you have to spend few minutes to hours to realize the solution because of many CLI commands and copy pastings. But did you know that you can replace author name and email in the entire git history with a script?

You don’t need to use git CLI commands to change the author's name and email in the commit history. You can just run a script to replace all the commits assigned to the wrong git author.

The following illustration shows the high-level solution. Using a script, you can replace the author. This script will

  • not change committed dates
  • not change committed messages

Warning* 👿 - This will change the git hashes of the commits.

Alt Text

Step (1/4): Create a fresh, bare clone of your repository

Bar clone means it does not have a git working directory. It has only .git data.

git clone --bare
cd repo.git

Step (2/4): Create file, update and execute

Open this script and change following OLD_EMAIL, CORRECT_NAME & CORRECT_EMAIL


git filter-branch --env-filter '

CORRECT_NAME="Your Correct Name"

' --tag-name-filter cat -- --branches --tags

Give executable permissions: You need permission to execute the shell script. You need to give executable permission
sudo chmod 755

Let's run:Time to run the shell script. This script will not push those changes to your origin. So, you can review after executing.

Step (3/4): Review your git log

Check author name, email, and commits messages.

git log
git log --decoreate --oneline      # one line log

Step (4/4): Push to Github

Check twice before you run the following command.
git push --force --tags origin 'refs/heads/*'

👿Warning👿 - Once you run this script, your commit hashes will be changed

Alt Text

because of hash changes - When you get a git pull, you will see something like this
Alt Text

Your git users will have to run following command to avoid that issue

git pull --allow-unrelated-histories


Also, read How to change git user for specific repo

Discussion (5)

daniguardiola profile image
Dani Guardiola_

Thanks, this seems useful, I'm saving it. Does it create an alternate history for the branch? It would seem to me like it does.

deshanm profile image
Deshan Madurajith Author • Edited

No. it just replaces author name and email in the entire git history. It does not change anything else like commit, branch, committed date.

Edit: It changes the hash. Thanks to @daniguardiola

daniguardiola profile image
Dani Guardiola_

So the commit hashes stay the same? I though the authorship was part of the hash pre-image

Thread Thread
deshanm profile image
Deshan Madurajith Author

Sorry, I was wrong. I just checked It changes the git hash. Thanks. I will update the post.

wapenshaw profile image
Siddharth Abbineni

Works great but , the script you made seems to change the hashes for every commit even if it doesn't match the if condition. It changes the hashes of the faulty commits as well as the commits where the email is correct.