As a programmer, you must have used Git for version control of your code.
But sometimes you might need to change your username in Git, either because you changed your username on your system or because you want to use a different username for a particular repository.
This can be done easily using the terminal. In this post, we will go through the steps to change your Git username in the terminal.
Check Your Current Git Configuration
Before making any changes, it is important to check your current Git configuration.
To do that, you can use the following command in the terminal:
git config --list
This will display your current Git configuration, including your username.
Change Your Git Username Globally
If you want to change your Git username globally, meaning for all the repositories on your system, you can use the following command:
git config --global user.name "NewUsername"
Replace “NewUsername” with the username you want to use.
Change Your Git Username for a Specific Repository
If you want to change your Git username for a specific repository, you can use the following command:
luaCopy codegit config user.name "NewUsername"
Replace “NewUsername” with the username you want to use.
Note that this will change the username only for the current repository, and not globally.
Verify the Changes
After making the changes, it is important to verify that the changes have taken effect.
You can do that by using the following command:
git config --list
This will display your updated Git configuration, including your username.
Push the Changes to the Remote Repository
If you have pushed commits with your old username to a remote repository, you need to force push the changes to update your username in the remote repository as well.
To do that, you can use the following command:
git push origin master -f
Replace “origin” with the name of your remote repository and “master” with the name of your branch.
Be careful when using the -f option, as it can overwrite the history of your remote repository.
Conclusion
In this tutorial, we have gone through the steps to change your Git username in the terminal.
Whether you want to change your username globally or for a specific repository, the process is straightforward and can be done easily using the terminal.
Remember to verify the changes and push the changes to the remote repository if necessary.




