Update user name/email of pushed commit in GIT

Sometimes we might need to update user name/email of pushed commit in GIT.
It might happen that we are working with multiple users in our local environment, and have pushed the commit with the wrong user. Or we might also want to update the username string to something more of our liking.

In this article, we will try try to understand how we can update the user’s name and/or email in an already pushed git commit

For this, let’s assume that you have pushed with user1 having email ID as user1@email.com
And we want to replace it with user2 having email ID user2@email.com

Let’s change directory to our project folder

> cd C:/<some-path>/my-test-project

Let’s first check what are the values set for the repo

> git config user.name
// user1
> git config user.email
// user1@email.com

The strings following the // are values stored in git. They would return value specific to the GIT user of the repository.

One can also run the same command with the –global flag to find information on the global user.

> git config --global user.name
> git config --global user.email

Now to update the username, we can run

git config user.name "user2"

And to set the user’s email, we can do

git config user.email "user2@email.com"

To do the same for the global user (Optional):

> git config --global user.name "user2"
> git config --global user.email "user2@email.com"

Updating the Commit

To update the commit, we need to run the –amend command without any changes

> git commit --amend

The commit –amend command needs to be run without any additional parameters, or without running any additional command.
And then push

> git push -f

-f is short for –force, and is required the force the push, as we haven’t made any commits for the git push to pick upon.

Associated Problems with doing this

The above method would work smooth if no one has yet pulled your changes before the amended commit.

But in case someone has pulled it, while it won’t be a problem with the commit, as it will be a plain merge due to no changes in the commit. But when that user pushes their changes, the commit history might show up the original commit that you ere trying to amend. Thus defeating the original purpose of doing all the work.

Read this for more tips and tricks on GIT.

Leave a Reply