Thursday, October 18, 2018

Change GIT Commit Author Name and Email for Specific Commits in History

If you are like me and have multiple GIT accounts that you work from, occasionally you may commit source code using the wrong GIT Profile/Alias. You will most likely not discover this until later when you look over your history at some point in the future and realise that some commits have a different Author Name and Author Email.

Fix a specific GIT commit using Interactive Rebase


In this short post I will show you how you can change the Author Name and Author Email from specific commits in your GIT commit history.

Lets assume you have this GIT Commit history.

commit 18e31d7cdec72d9d6aba0ef19e5270d14936b511 (HEAD -> master, origin/master)
Author: Mark Paul <another-email@gmail.com>
Date:   Thu Oct 18 15:35:52 2018 +1100


    Committed bug fixes



commit 3546dd57f77508d9a6262af8b862dff23422ba72

Author: Mark Paul <another-email@gmail.com>

Date:   Mon Oct 15 20:56:32 2018 +1100



    init the code



commit e5b47248597e2df98a106f098311afc34f5cc37d

Author: Mark Paul <my-email@gmail.com>

Date:   Mon Oct 15 20:51:22 2018 +1100


In the above commit history, Mark Paul <my-email@gmail.com> is the profile you WANT to use. But you realise that you also have commits using the INCORRECT Mark Paul <another-email@gmail.com> profile.

So you need to update the 3546dd57f77508d9a6262af8b862dff23422ba72 and 18e31d7cdec72d9d6aba0ef19e5270d14936b511 commits to use the Mark Paul <my-email@gmail.com> git profile.

You do this by using GIT's Interactive Rebase feature.

Interactive Rebase off of a point earlier in the history than the commit you need to modify (git rebase -i <earliercommit>). In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify. Then when git prompts you to change the commit, use this:

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

Let's see this in action:

In the example above, our commit history was e5b4724-3546dd5-18e31d7 with 18e31d7 as HEAD, and you want to change the Author Name and Author Email of 3546dd5 and 18e31d7, then you would:

Specify git rebase -i e5b4724 (use the full commit hash if the short commit hash does not work)

If you need to edit e5b4724, use git rebase -i --root

Change the lines for both 3546dd5 and 18e31d7 from pick to edit

Once the rebase started, it would first pause at 3546dd5

You would git commit --amend --author="Mark Paul <my-email@gmail.com>"

Then git rebase --continue

It would pause again at 18e31d7

Then you would git commit --amend --author="Mark Paul <my-email@gmail.com>" again

The git rebase --continue

The rebase would complete.

Use git push origin master -f to update your origin with the updated commits.


Hope this helps you.

See this for more details on this topic.


Happy Coding / Hacking!

No comments:

Post a Comment

Fork me on GitHub