10

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?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Ore0D3v
  • 159
  • 1
  • 2
  • 11
  • git user can be configured per repository. – choroba May 15 '18 at 15:56
  • 4
    Possible duplicate of [Multiple GitHub Accounts & SSH Config](https://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config) – phd May 15 '18 at 16:03
  • Possible duplicate of [Multiple git user in single device](https://stackoverflow.com/questions/50304606/multiple-git-user-in-single-device) – Frax May 15 '18 at 16:09

5 Answers5

11

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 = yourownemail@yourprovider.com

…

[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.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • 1
    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. – saulius2 Jun 24 '22 at 17:00
4

You can configure your user details per repository.

git config user.name 'Your Name'
git config user.email 'name@example.com'

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

choroba
  • 231,213
  • 25
  • 204
  • 289
3

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.

Alan Aranda
  • 202
  • 3
  • 14
3

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 yourusername@github.com as this creates a git:https://yourusername@github.com 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.

sceee
  • 1,681
  • 19
  • 34
1

Git multiple user config manager gum

Install

$ npm i -g @gauseen/gum

Example

$ gum list

Currently used name=gauseen email=gauseen@gmail.com
┌────────────┬─────────┬─────────────────────────┐
│ group-name │    name │                   email │
├────────────┼─────────┼─────────────────────────┤
│    global  │ gauseen │ gauseen@gmail.com       │
│    user1   │ li si   │ lisi@gmail.com          │
│    user2   │ wang er │ wanger@gmail.com        │
└────────────┴─────────┴─────────────────────────┘
$ gum use user1

Currently used name=li si email=lisi@gmail.com
cc gau
  • 21
  • 1