How to Configure Git Username and Email: Step-by-Step Guide

Hey developers!

Before contributing to a codebase, one of the first things you need to do is configure your Git username and email.

These settings act as your digital signature, ensuring your commits are properly attributed to you.

Configuring your Git username and email ensures that each commit is tracked with your name, making it easier to identify who made which changes when troubleshooting, reviewing, or even figuring out who broke production! 😂

In this step-by-step guide, you’ll learn how to configure your Git username and email both globally and for a single repository.

Let’s get started!

 How to Configure Git Username and Email Globally

Steps to follow:

1. Set your Git username globally on your device:

Run the below command:

git config --global user.name "My name"

Replace "My Name" with your username (use quotes for multi-word names)

2. Set your Git email globally on your device:

Run the below command:

git config --global user.email "johndoe@email.com"

Replace "johndoe@email.com" with your email. Note that the quotes need to remain the same.

3. Verify your settings globally:

Run the below commands to check your git configuration:

git config --global user.name

git config --global user.email

These commands will output the username and email configured on your device, they should match the username and email you entered in the above commands.

Note: You can run these commands from anywhere in your system, they’ll apply globally to all repositories.

P.S: Don’t configure the wrong username and email to confuse the QA team 😜

That's all!

You’ve successfully set your Git username and email globally on your device and are ready to break production 😂

How to Configure Git Username and Email for a Single Repository

Steps to follow:

First, navigate to your repository:
Ex: cd ~/desktop/my-project

Note: Replace ~/desktop/my-project with the actual path to your Git repository folder.

1. Set Your Git User name for a single repository:

Run the below command:

git config user.name "My Name"

Note that the command does not include the –globally flag.

Omitting the –globally flag allows you to set your username and email for a single repository. Make sure you are running the command after checking out to the repository in your terminal

2. Set Your Git Email for a single repository

Run the below command:

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

The same configuration works for setting up the email as well. Just omit the –globally flag and replace user.name with user.email

3. Verify Your Git Username and Email:

Run the below commands to check your git configuration for this specific repository:

git config user.name

git config user.email

These commands will output the username and email configured on your repository, they should match the username and email you entered in the above commands.

That's it!

You’ve successfully configured your Git username and email for a specific repository

Conclusion:

With these simple steps, you can easily manage your Git settings globally within your device or for a single repository, making collaboration on projects much smoother.

Keep your settings updated to reflect the correct user information, especially if you work on multiple GitHub accounts.

Frequently Asked Questions [FAQs]

1. Where are the global Git configuration settings stored?

Global Git configuration settings are stored in a file called .gitconfig located in your home directory. Mostly this file is located at:

Linux or macOS: ~/.gitconfig

Windows: C:\Users\YourUsername\.gitconfig

This file contains user-specific configurations like your username, email, and other Git settings that apply globally across all repositories.

2. Where are the repository-specified Git settings stored?

Repository-specific Git settings are stored in a .git/config file within the root of the repository. These settings only apply to that particular repository.

3. What's the difference between global and local Git configuration?

Global configuration:

  • Applies across all Git repos on your system
  • Stored in ~/.gitconfig (or  C:\Users\YourUsername\.gitconfig on Windows)
  • Default settings like user.name, user.email

Local configuration:

  • Applies only to the specific Git repository.
  • Stored in .git/config within the repo
  • Can override global settings for that repo
  • Different repos can have different usernames and emails

The key difference is scope - global applies system-wide, local applies it within the repo. When both are set, the local configuration overrides the global configurations.

Tip: it’s a good practice to add .git/config to your .gitignore to avoid pushing local configurations.

Bonus tip: You can edit `~/.gitconfig` (global) or `.git/config` (local) directly to update configuration using the text files as well. It will also have the same impact if you forget the commands.

Still have questions?

Send an email to archana@leadwalnut.com, OR

Book a FREE consultation with an expert developer here.