Open In App

How To Use Multiple Users With Git?

Last Updated : 02 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git is an important tool for version control in software development. Whether you are contributing to open-source projects, working on personal projects, or part of different organizations, managing multiple Git users becomes necessary. In this article, we will learn how To Use Multiple Users With Git.

Approach 1: Using SSH Keys

SSH keys are a secure way to authenticate with Git repositories without using passwords. By generating multiple SSH keys, you can link each key to a different Git user, allowing you to switch between users seamlessly.

Step-by-Step Guide for Setting Up SSH Keys:

1. Generate a new SSH key:

Open your terminal and use the following command to generate a new SSH key:

ssh-keygen -t rsa -b 4096 -C "[email protected]"
file
ssh-keygen

2. Add the new SSH key to the ssh-agent:

Start the ssh-agent and add your SSH key:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

3. Add the SSH key to your Git account:

Copy your SSH key to the clipboard:

cat ~/.ssh/id_rsa.pub

Then, add the key to your Git account settings on GitHub, GitLab, or your preferred Git service.

4. Configure multiple keys in the SSH configuration file:

Edit the ~/.ssh/config file to manage multiple SSH keys:

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal

Host work.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work

5. Clone repositories using the specified host:

git clone [email protected]:username/repository.git
git clone [email protected]:username/repository.git

Approach 2: Configuring Git Config:

Git provides a flexible way to configure user information per repository using the .gitconfig file. This method involves setting up global and local configurations, making it easy to manage user details across various projects.

Step-by-Step Guide for Configuring Multiple Git Users in Git Config:

1. Set a global Git user:

Use the following commands to set a global Git user:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

2. Override user information for a specific repository:

Navigate to your repository and run:

git config user.name "Work Name"
git config user.email "[email protected]"

3. Verify your configuration:

To check your current configuration, use:

git config --list

Approach 3: Using Git Credential Helper:

Git Credential Helper stores and manages credentials for different repositories. This tool automatically provides the correct credentials when accessing different repositories, simplifying the process of handling multiple Git users.

Step-by-Step Guide for Using Git Credential Helper for Multiple Users:

1. Enable Git Credential Helper:

Use the following command to enable credential storage:

git config --global credential.helper store

2. Set credentials for different repositories:

When accessing a repository for the first time, Git will prompt you for your username and password. These credentials will be stored for future use.

3. Manually edit the credentials file:

Your stored credentials are saved in ~/.git-credentials. Here is an example of how this file might look:

https://username:[email protected]
https://work_username:[email protected]

Next Article
Article Tags :

Similar Reads