Blogs

How to Change Authors of Pushed Git Commits

July 1st, 2021

“In the world of software engineering, mistakes are inevitable, but what really matters is how you overcome those mistakes, learn from them and strengthen yourself.”

Why I Chose to Write This Blog

One night last week, I switched between my work-related repository and personal repository. I made several git commits in each repository before pushing those changes to git in their respective repositories. After that, everything was fine, and both the repositories were working fine.

Friday evening, when I completed my tasks for the week and went to raise a pull request for my code to be pushed to the work-related repository, I found the mistake I had made. As I looked at the author details of the commits I made included the name and email configured for my personal repository.

Here is a snapshot of what my commit history looked like.

 

For this blog, I will walk through the steps I have performed to correct coolpran4u to Pranjal Goswami (reference image above). You may use the same steps if you are in the same situation. (Now that you are reading this blog, there is a 77% chance that you might have encountered similar situations at some point in time).

At least for me anyway, and as you might have already guessed, the very first step was a panic attack — “Ah! What have I done!?”

Step two is the more logical step, take a deep breath and tell yourself that you are not the first to make this mistake, and there must be someone on the internet who did the same thing and overcame it. After all, people managing git are smart enough to have predicted such incidents and may have already prepared a solution to resolve such a problem. So, with this positive attitude, let’s do a bit of googling!

Solution: Correct Git Commit Author Details

Well, these kinds of mistakes happen, and there is no need to worry. There is a way to correct your git commit details. If you have pushed just one commit with the wrong author details, then you can correct your last commit details using the following:

git commit --amend --author="<firstName lastName> <email@email.com>"
git push

If there is more than one commit that needs to be corrected, it becomes tricky. First, you need to use the strongest git command called rebase. Rebase is the master of everything in git. You probably know about it already, but you should use it at a minimum because it can mess things up badly if misused. You can find more on rebase here.

There are five easy steps to iteratively correct the author details:

  1. Point the repository to the last correct commit so that all commits after that can be corrected. Use the following rebase command to do that:
    git rebase –i <last-correct-commit-hash>
  2. The above command will display a list of commit hashes after the specified commit (ordered by newest to oldest). This list usually opens in a vi/vim view, as shown below.
    commit list in vim view

    Press “i” for editing the view. Change the word pick to edit for each commit that needed to be corrected. Then save and quit (!wq).

  3. Once you save the changes and quit, the rebase process will start, and you should be able to correct the authors of each commit one after another using the following command:

    git commit --amend --author="<Author Author> <email@email.com>"

  4. Once you have corrected one commit, go to the next commit and use the following command:

    git rebase –continue

  5. Finally, once all the commits are corrected, you should be good to push your changes using the following command:

    git push

Hurray! You have done it! All of the bad commits are now corrected.

I hope you liked the blog, and it helped resolve your issue.

A Tip from the Author

As a precautionary measure, it is advised to set authors per repository rather than at the global level to avoid messing up author credentials across the entire repository. You may want to run the following commands by opening a terminal inside your respective repositories:

git config user.email "repos_email@repo_email.com"
git config user.name "repo_AuthorFirstName repo_AuthorLastName"

about the author

Pranjal Goswami

Pranjal Goswami is a Module Lead on the Product development team and is responsible for managing the Arena platform and core features. He has been with Zaloni since October 2015 and plays a key role in developing and managing Arena. His technical expertise includes Java technologies, Apache Hive, Hadoop, AWS services, and Relational Databases. Pranjal holds a Bachelor's degree in Information Technology and has completed many technical certifications.

zaloni zine header