git username and password for different accounts

How to Setup Git Username & Password for Different Repos

Git allows you to work with multiple repositories at the same time on a single system using a single git client. Sometimes you may need to connect to different cloud services such as GitHub, Gitlab, etc. each of which has different username and password. But it can be tedious to manually enter different user credentials for each repo. In such cases, you will need to setup git username & password for different repos. Once you do this, your local git client will automatically use the appropriate user credentials depending on the repo you are working with. In this article, we will learn how to do this.

How to Setup Git Username & Password for Different Repos

When you need to manage multiple git users on a single machine, it is advisable to use SSH service for this purpose.

1. Create SSH Key

Open terminal and run the following command to create an SSH key. Replace email_address with your git user’s email address.

$ ssh-keygen -t rsa -C "email_address"

When prompted, save the file as id_test or something else as per your requirement. We will save this file to ~/.ssh/id_test

2. Copy the SSH Key

Next, copy this SSH key to your cloud service such as GitHub or Gitlab and add it to your ssh service with the following command.

$ ssh-add ~/.ssh/id_test

3. Create Config File

Next, you need to create config file at ~./ssh with the following contents. Replace <host identifier>, <host_domain>, <username>, <ssh_key_location> with an identifier for repo, host’s domain name name, your git username and location of above SSH file respectively.

Host <host identifier>
  HostName <host_domain>
  User <username>
  IdentityFile <ssh_key_location>

Here is a sample configuration for GitHub.

Host github-company
  User git
  IdentityFile ~/.ssh/id_test

You can add information about each repo in this file one below the other as shown below.

Host host1
  User git
  IdentityFile ~/.ssh/file1

Host host2
  User git
  IdentityFile ~/.ssh/file2


Save and close the file.

Depending on your requirement, you may use the same or different SSH key files for all your repositories.

4. Add Remote

Run the following command to add remote URL to your git client. And finally, we push the local repo to remote repo URL. Replace username with your GitHub account’s username for the repo.

$ git remote add origin git@github-company:username/repo.git
$ git push origin master

Please note, instead of pushing to we are pushing to git@github-company where github-company is the host we have mentioned in above config file.

Similarly, you can modify the push URL according to the service where you want to push your repo.

In this article, we have learnt how to setup user credentials for different repos. Once you setup the SSH key, config file and remote URL, then whenever you push to/pull from repo, git will automatically provide the proper credentials depending on the repository you are in and its remote URL.

Also read:

How to Reset Git Credentials
How to Add Images to on GitHub
How to Cache HTTPS Credentials for Pushing Commits
How to Change Author & Committer for Multiple Commits
How to Clone Subdirectory of Git Repository

Leave a Reply

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