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:
- “Locally”: Setting Username and Email for a Single Git Repository.
- “Globally”: Setting Username and Email for All Git Repositories.
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:
Step 2: Setting Username Locally
Then, set a username locally by utilizing the following command and specify the desired username:
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:
Step 4: Verify Username
Next, ensure that the username has been set correctly by running provided command:
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:
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:
Step 2: Set Email Globally
Then, set the specific email for all Git repositories:
Step 3: Verify Username
Now, verify whether the global username has been set correctly:
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:
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:
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:
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”.