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 firstname.lastname@example.org
And we want to replace it with user2 having email ID email@example.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
> git config user.email
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 "firstname.lastname@example.org"
To do the same for the global user (Optional):
> git config --global user.name "user2" > git config --global user.email "email@example.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.