Connect with us

Git

How to Change Git Config Username and Email?

Spread the love

Git uses your username and email address to track changes you make in a repository. These details are part of each commit you create and are essential for collaboration and traceability. If your username or email needs to be updated—perhaps due to a new email address, organization change, or a typo—it’s easy to change them in Git’s configuration.

In this blog, we’ll walk through how to change your Git username and email at the global and local levels, ensuring your commits reflect the correct information.

Understanding Git Config Levels

Git configuration operates at three levels:

  1. System Level: Applies to all users on the machine. Configurations are stored in /etc/gitconfig.
  2. Global Level: Applies to a specific user across all repositories. Configurations are stored in ~/.gitconfig.
  3. Local Level: Applies only to a specific repository. Configurations are stored in the .git/config file inside the repository folder.

Steps to Change Git Username and Email

1. Update Global Username and Email

If you want to change your username and email for all repositories you work on, use the --global flag.

Commands:

To update the username:

git config --global user.name "Your New Username"

To update the email:

git config --global user.email "[email protected]"

Example:

git config --global user.name "Jane Doe"
git config --global user.email "[email protected]"

2. Update Local Username and Email

If you only want to change the username and email for a specific repository, omit the --global flag.

Commands:

To update the username for the current repository:

git config user.name "Your New Username"

To update the email for the current repository:

git config user.email "[email protected]"

Example:

git config user.name "John Developer"
git config user.email "[email protected]"

3. Verify Your Changes

To confirm that your changes were applied, use the following command:

  • For global configuration: git config --global --list
  • For local configuration: git config --list

Output Example:

user.name=Jane Doe
[email protected]

If you’re in a repository, the git config --list command will show both global and local settings, with local settings overriding global ones.


Tips for Configuring Username and Email

  1. Use a Consistent Email for Collaboration:
    Ensure the email matches the one associated with your Git hosting platform (e.g., GitHub, GitLab). Mismatched emails can cause commits to not be linked to your account.
  2. Use Environment-Specific Configurations:
    If you work on multiple projects with different usernames or emails (e.g., personal and work), configure them locally for each repository.
  3. Check Commit History:
    After changing your email or username, your previous commits will not be updated. They will still display the old information, as commit metadata is immutable.
  4. Use Professional Credentials:
    For work-related repositories, use your professional name and work email to maintain professionalism.

Common Use Cases

Changing Credentials for Personal and Work Repositories

If you contribute to both personal and professional repositories, set your global configuration for personal use and configure work repositories locally:

  • Global (for personal projects): git config --global user.name "Personal Name" git config --global user.email "[email protected]"
  • Local (for work repository): git config user.name "Work Name" git config user.email "[email protected]"

Troubleshooting

Issue: Email Not Recognized on Git Hosting Platforms

Ensure that the email you configure is verified on your Git hosting platform. For example, on GitHub:

  • Go to Settings > Emails.
  • Add and verify the new email.

Issue: Old Credentials Being Used

If Git is still using old credentials, check if you have multiple configurations in place:

  • Run git config --list --show-origin to see where configurations are stored.
  • Update or delete the outdated configuration files.

Conclusion

Updating your Git username and email is a straightforward but crucial step in ensuring accurate attribution of your contributions. Whether you’re making changes globally or for specific repositories, following these steps will help you maintain consistency and professionalism in your commits.

Take a moment to review and update your Git configuration today to avoid any attribution issues in the future.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *