How can I have multiple git accounts on a single development machine?

10,738

Solution 1

You can store your credentials in each project using.

git config credential.helper store
git push origin HEAD
write user and password

Go to the project folder and type the git commands.

Solution 2

By default, git is using a system-wide configuration file or the one stored at top of your home directory.

But you can also set a file .git/config inside each repository, either by editing manually or using git config. Inside, you may specify the following sections :

[user]
    name = Your name
    email = [email protected]

…

[credential "<your_project_hoster_url>"]
    username = <username to log in with>

You can check git config in general or git credentials in particular for more information about it.

Solution 3

You can configure your user details per repository.

git config user.name 'Your Name'
git config user.email '[email protected]'

GitHub can be configured to recognise several ssh keys assigned to email addresses and send email messages to the corresponding ones when needed.

Solution 4

If you prefer https instead of the SSH key solutions, the following works on Windows at least for Github (and in connection with TortoiseGit using a credential manager):

  1. In your local repository (or while cloning), set the remote (origin) url to
https://[yourusername]@github.com/[org]/[repo].git

(The important part here is the [email protected] as this creates a git:https://[email protected] entry in the Windows credential store that differs from the regular git:https://github.com entry that holds the credentials for all "regular" remote urls)

In case you want to edit it for an existing repository via the repo/.git/config file, this is something like

[remote "origin"]
    url = https://[yourusername]@github.com/[org]/[repo].git
    fetch = +refs/heads/*:refs/remotes/origin/*
  1. Additionally, set the credential and user properties in the repo/.git/config file so your commits have the correct author:
[credential]
   username = [Github-Username]
[user]
   name = [Fullname]
   email = [Email Address]

At your git remote operation (fetch, push etc.), you will be asked for authentication depending on your auth manager.

Share:
10,738
Ore0D3v
Author by

Ore0D3v

Updated on June 17, 2022

Comments

  • Ore0D3v
    Ore0D3v almost 2 years

    I am trying to work simultaneously on more than one (two or three) GitLab (or even GitHub) projects on a single development machine. Because upon configuration the IDEs and the git service has the data of my primary user when I try to checkout or clone another project with a different username / password the system says either project is not found or I do not have permissions to do that.

    How can I configure and use more than one git user on a single development machine?

  • Ore0D3v
    Ore0D3v almost 6 years
    thank you so much! I already granted the first as answer but all worked like a charm!
  • 0andriy
    0andriy over 2 years
    Won't work if you have many remotes in one repo.
  • saulius2
    saulius2 almost 2 years
    Thanks for the hint! Finally I found the culprit of Git using the wrong username from my ~\.git-credentials (from the first line, but not from the second one). So I just set this setting locally: git config credential.username my-2nd-username, and it worked fine. Thank you.