DEV Community

Cover image for Change a Commit's Author on Git
Mohammad-Ali A'RÂBI
Mohammad-Ali A'RÂBI

Posted on • Originally published at Medium

Change a Commit's Author on Git

I usually commit to my work and to my private git projects, and I use two different email addresses for them. But it happens sometimes that I mess up the emails and commit to the work project with my private email address or vice versa.

So, it comes in handy if one can change the last commit’s author, although my use case is not the only case changing the commit’s author can be useful.

Change the Last Commit’s Author

It would be a lot easier if you want to change only the last commit’s author. It’s usually easier if you want to change the last commit’s anything, generally. This can be achieved with the good old amendment:

git commit --amend --author="Mohammad-Ali A'RÂBI <mohammad-ali@aerabi.com>"
Enter fullscreen mode Exit fullscreen mode

After this command is executed, an editor opens up so you’ll have a chance to change the commit message as well.

Be careful not to have any changes staged before doing the amendment, otherwise, those changes will be added to the commit as well.

Also, beware that you have to push with force with lease now, as you have done some housekeeping in the history:

git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

Change the Author in an Older Commit

Now, let’s assume we want to change the author of the commit which is 2 commits behind (so, the 3rd commit, counting from the last). To find the commit we want to edit, let’s do a oneliner git log:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

The output looks like this:

dd16fac (HEAD -> master) Add GitHub page 
b8473cf Update README
deba6a0 Add REAME
Enter fullscreen mode Exit fullscreen mode

Here, Add GitHub page is the last commit, and we want to edit Add README. To do so, we use the interactive rebasing:

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

The HEAD~3 indicates how far we want to look back and change history. In this case, it’s 3 commits. By running this command, an editor opens up with the following content:

pick deba6a0 Add README 
pick b8473cf Update README
pick dd16fac Add GitHub page
Enter fullscreen mode Exit fullscreen mode

Note that the order is different from the order in the git log. The commit you want to change is now represented in the first line. To change its author, you want to change the verb in front of it, from pick into edit.

edit deba6a0 Add README 
pick b8473cf Update README
pick dd16fac Add GitHub page
Enter fullscreen mode Exit fullscreen mode

Then save the file and exit. You will return to the command line and have the chance to amend the commit you chose:

git commit --amend --author="Mohammad-Ali A'RÂBI <mohammad-ali@aerabi.com>"
Enter fullscreen mode Exit fullscreen mode

This is the same as before. Now let the interactive rebaser know that you’re done here and want to continue:

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

As you have just changed the author, there will be no conflicts and your rebase will finish successfully. Now, as before, you have to push with force with lease.

Conclusion

This tutorial was focused on changing an author’s email address, but one can use the same instructions to change the author’s name or perform any kind of amendment.

Today’s main takeaway is the following:

Check your email address before committing. It’s a lot easier.

You can also add CI jobs that check the author’s names and email addresses: It’s better to fix the commit’s authors before they are merged into master. Generally, the sooner you fix stuff, the easier it is to fix them.

Top comments (0)