How to Change the Author of All Commits in a Git Repository
Git/
To change the author name or email of existing commits, you will have to rewrite the history of your repository.
Rewriting history can introduce problems, so you shouldn’t do in a public repository that other people work on.
If you have certainly decided to overwrite the commit history, you can use a tool called git filter-repo to achieve your goal.
Changing Commit Author With git filter-repo
Git filter-repo is a tool for rewriting the history of your repository. You can use it to change the author of past commits and much more.
It is a replacement for git filter-branch, which doesn’t require installation, but is outdated and much slower. The next section in this post shows how to use git filter-branch, but git filter-repo is the better choice.
Git filter-repo requires at least Git version 2.22.0, which you can verify with the following command:
git --version
You have to install git filter-repo using your system’s software package management system:
-
Ubuntu
sudo apt install git-filter-repo
-
MacOS
brew install git-filter-repo
-
Fedora
sudo yum install git-filter-repo
For other systems, follow the installation guide here.
Since git filter-repo works best on a fresh clone of your repository, you have two options:
-
Clone your repository from the existing local version
git clone your-project your-project-copy
-
Or, create a new clone from the remote repository (GitHub, GitLab etc.)
git clone remote-repo-url your-project-copy
Substitute your-project
with the name of the repository you want to edit.
Git filter-repo uses a mailmap file to update commit author information.
Create this file in the parent directory outside your repository.
nano mailmap
And add the following information to the mailmap file:
New Name <[email protected]> <[email protected]>
This will change the name to New Name
and email to [email protected]
of the author of all commits made by the author with [email protected]
email address.
Your final file structure should look like this:
your-project/
your-project-copy/
mailmap
Before you rewrite the author data, you can use git log
to see the current information. Press q
to exit the command.
When you’re ready, switch to the repository you want to edit and run the git filter-repo
command.
cd your-project-copy
git filter-repo --mailmap ../mailmap
The --mailmap
argument accepts the path to the mailmap file you created earlier.
After the command is done running, you can run git log
and confirm that the name and email has been changed.
Git filter-repo deletes the remote address of your repository to protect the remote from accidental overwrites.
To change the author of pushed commits to a remote, start by adding back the remote address.
Either create a new remote repository or use the address of your current one.
git remote add origin address-of-the-remote
Do not force changes in remote repositories that can affect other people’s work!
But, if you want to overwrite your existing remote repository, you have to force it with git push -f
.
Using git filter-branch
If you don’t want to install anything to change commit author data, you can use the built-in git filter-branch tool.
Be aware that it is doesn’t work as well as git filter-repo and is much slower. Make sure you have a working copy of your repository first.
But, if you’ve made your choice, here’s the command you can use. Substitute the values of OLD_EMAIL
, NEW_EMAIL
and NEW_NAME
with your own data.
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
NEW_EMAIL="[email protected]"
NEW_NAME="John Doe"
if test "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL"
then
GIT_AUTHOR_EMAIL=$NEW_EMAIL
GIT_AUTHOR_NAME=$NEW_NAME
fi
if test "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL"
then
GIT_COMMITTER_EMAIL=$NEW_EMAIL
GIT_COMMITTER_NAME=$NEW_NAME
fi
' -- --all
When you run this command, you will see a warning. You have to wait a few seconds before the script starts running.
Like mentioned before, git filter-repo is the better tool for this job, so you have the option during this time to press CTRL + C and give a shot.
To apply your changes to a remote, you will need to either force push the changes or create a new remote repository.