DEV Community

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

Posted on • Updated on

Replace accidentally committed git author information from git history

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 https://github.com/user/repo.git
cd repo.git

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

Open this script and change following OLD_EMAIL, CORRECT_NAME & CORRECT_EMAIL

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.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

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

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.
./script.sh

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

References

Also, read How to change git user for specific repo

Top comments (5)

Collapse
 
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.

Collapse
 
deshanm profile image
Deshan Madurajith • 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

Collapse
 
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

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

Collapse
 
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.