Change Author on Git Commits on Entire GitHub Repository
Learn how to change the email address and name on all commits in a GitHub-Hosted Repository.Step One: Clone Repository as Bare
Most people host their repository on a remote hosting service like GitHub. This article will assume you have a Git repository hosted on GitHub.
Even if you have the repository already on your machine, you need to clone it again as a bare repository for this to work.
git clone --bare git@github.com:username/repository.git
This will clone the repository and put it in a folder of the same name with .git on the end to indicate it is a bare repository. If you specified an optional dir name at the end of the above comand, the folder name will be exactly that.
Step Two: Run Script to Change Author Info
Below is a script you can run to change the author details. It works by finding all commits with the old info, and updating that info with the new details.
Firstly though, you must change the parts at the top of the script to match your situation.
git filter-branch --env-filter '
OLD_EMAIL="your.name@email.tld"
CORRECT_NAME="Your Name"
CORRECT_EMAIL="your.name@newemail.tld"
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
Copy the above into a text editor of your choice. Modify the details at the top, and then copy and paste it into your terminal / command prompt and hit enter to run the script.
You may get a warning, in my experience it is usually safe to ignore this warning. After a moment, it should start rewriting all the commits in the repository.
Confirm these have changed successfully by checking the log.
git log
Step Three: Push To Remote
To make sure these changes take effect not only in our local repository but also our remote one on GitHub, we need to push them to GitHub.
Since we made changes to existing commits, we need to forcefully push these changes to GitHub.
git push --force --tags origin 'refs/heads/*'
Note that the 'origin' above refers to whatever name you have given your remote, if you have not changed this it will be 'origin' otherwise you will need to update this to match the correct remote name in your case.
Once you have executed the above command, you can safely remove the repository directory from your machine.
If you already had the repository on your machine before making these changes, to make sure the changes properly reflect that, you may want to delete the repository directory and re-clone it as you normally would.
Happy Gitting!