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!
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> <firstname.lastname@example.org>" 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:
git rebase –i <last-correct-commit-hash>
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).
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@example.com>"
Once you have corrected one commit, go to the next commit and use the following command:
git rebase –continue
Finally, once all the commits are corrected, you should be good to push your changes using the following command:
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.
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"
News By: Team Zaloni
Blogs By: Matthew Caspento
Blogs By: Haley Teeples
Blogs By: Team Zaloni
Blogs By: Amy King
Blogs By: Pranjal Goswami