Git

Can I Specify Multiple Users for Myself in .gitconfig?

On Git, developers work and deal with multiple projects concurrently. For this purpose, each project may need different usernames and email. In this case, you can set multiple users in different Git scopes. More specifically, Git scopes include local scope and global scope. The local scope settings apply only to the specific repository. In contrast, the global scope settings apply to all repositories.

This blog will illustrate how to specify multiple users in “.gitconfig”.

Is it Possible to Specify Multiple Users for Ourselves in .gitconfig?

Yes, you can specify multiple users for yourself in the “.gitconfig” as:

How to Setup Username and Email Locally for a Single Git Repository?

To set up the username and email for a single repository, the “git config user.name “<user-name>”” and the “git config user.email “<user-email>”” commands can be used respectively.

Step 1: Navigate to Particular Repository

First, write out the following command and redirect to the local repository:

cd "C:\Git\Repo2"

Step 2: Setting Username Locally

Then, set a username locally by utilizing the following command and specify the desired username:

git config user.name "Amna Ali"

Make sure to replace the user’s name with the required one:

Step 3: Setting Email Locally

After that, set the desired email address for a particular repository as we did here:

git config user.email "[email protected]"

Step 4: Verify Username

Next, ensure that the username has been set correctly by running provided command:

git config user.name

It can be observed that our desired username has been set, which is “Amna Ali”:

Step 5: Verify Email

Type out the given-below command to verify the locally configured email:

git config user.email

As you can see the specified email has been set:

How to Setup Username and Email Globally for All Git Repositories?

To set up the username for all Git repositories, run the “git config –global user.name “<user-name>”” command, and for email, execute the “git config –global user.email “<user-email>”” command.

Step 1: Set Username Globally

Utilize the below-stated command along with the “–global” option and specify the desired username to set it for all Git repositories:

git config --global user.name "Laiba Younas"

Step 2: Set Email Globally

Then, set the specific email for all Git repositories:

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

Step 3: Verify Username

Now, verify whether the global username has been set correctly:

git config --global user.name

It can be seen that the particular username has been set for all repositories, i.e., “Laiba Younas”:

Step 4: Verify Email

After that, view the globally set email:

git config --global user.email

It can be observed that the email for all Git repositories has been set successfully:

Step 5: Verify Changes

Check Git log to view changes:

git log

In the below output, it can be seen that commit history contains commits of different users:

Step 6: View Configuration Settings

Lastly, list the configuration settings of the “.gitconfig” file through the below-stated command:

git config --list

The below-highlighted part indicates that the “.gitconfig” contains two usernames and emails. One username and email for a single (particular) repository, and the other one is for all Git repositories:

That was all about specifying multiple users in “.gitconfig”.

Conclusion

Users are allowed to specify multiple users in the “.gitconfig”. It can be done “locally” by setting username and email for a single Git repository and “globally” by setting username and email for all Git repositories. To set up the username and email locally, the “git config user.name “<user-name>”” and the “git config user.email “<user-email>”” commands can be used, respectively. Moreover, the same command with the “–global” option is utilized to set username and email globally. This blog illustrated the method to specify multiple users in “.gitconfig”.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.