Have you ever had a situation where you needed to change the author name of a git commit? For a novice developer, working with git can be something of a black magic. It’s not entirely clear where the code is being downloaded or how git can identify you.
If you don’t know what git is, see our article describing 👉 what is a Git repository and why it is so important 👈
There are situations when newcomers to the art of development don’t get the setup right. After adding an SSH key to their account they start working and commit their code to the repository.
How to change git commit author name and email?
How big must be the surprise of such a user when opening a browser version of the repository they see the author John Doe instead of his name. Here is the solution on how to change the author of a git commit.
Set git config correctly
The first step is to set the correct first name, last name, and email of the author, which is yours:
$ git config --global user.name "John Doe" $ git config --global user.email email@example.com
Let’s change the git commit author’s name and email
Make sure you are on the current branch and have locally downloaded all changes from the repository. In the last step we need to overwrite all previous commits on the remote repository, so when you don’t have the current changes locally you will overwrite the latest changes!
In the next step, create a file in the root directory –
git-author-rename.sh (we will execute the whole script in a file to avoid problems with proper order or wrong spaces/entries).
Paste the following code into the file:
#!/bin/sh git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then GIT_AUTHOR_EMAIL=correct@email; GIT_AUTHOR_NAME="Correct Name" GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL; GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all'
Replace the following statements in the above script:
- incorrect@email – enter your incorrect email that you entered earlier
- correct@email – enter your correct email that you set in the global config
- Correct Name – enter your correct name which you have set in global config
After you make sure everything is correct run the script from terminal/console using the command:
Depending on the project, it may take a while to change the author of a commit, but after a while, all the commits that were sent in error should have the updated author and his email.
Push changes to a remote repository
Once the data swap process is complete, all we need to do is push all changes to the remote repository with the
git push -f
That’s it, now on the remote repository, your commits will no longer be signed by John Doe 🎉