10

I recently created a second GitHub account two separate my work and my private projects (before, I only had the work account). I use https in combination with the Windows credential storage. To automatically select the correct account, I store my private account info in ~/.gitconfig and the work account info in ~/work/.gitconfig as suggested here.

Unfortunately, when I try to push changes in my private repositories, I get the following error:

$ git push
remote: Permission to privateuser/privaterepo.git denied to workuser.
fatal: unable to access 'https://privateuser@github.com/privateuser/privaterepo.git/': The requested URL returned error: 403

I set the remote URL to git remote set-url origin https://privateuser@github.com/privateuser/privaterepo.git like suggested here. Pushing in my work repos still works fine. And when I type git config user.name in my private/work repos, I get my private/work username, respectively - as it should be.

What's the problem with the new private repositories? Why does git still think I'm workuser, when I try to push to my private repos? Does it have to do something with the Windows Credential storage, which I used to store my work credentials? It never asked for the password of my private account...

stefanbschneider
  • 5,460
  • 8
  • 50
  • 88
  • How about using `git config credential.authority Basic` or `git config credential.useHttpPath true` – Yousha Aleayoub Aug 03 '20 at 16:25
  • Just prefix the origin path with your username and @, like so: https://MyAccount@github.com/repo/reporepo.git This allows the Windows credential manager to store multiple logins. *Make sure you're logged out when you attempt to pull from the new repo for the first time, so that it will ask you to log in and allow you to select the different account.* No .gitconfig or git/config necessary. – AyCe Jul 12 '22 at 13:35

4 Answers4

7

The conditional include that I detail here is only for commit authorship (user.name/email).

This has nothing to do with authentication (credentials: username/password)

Those are probably cached in a credential manager (like the linux osx-keychain)
Check the output of:

git config credential.helper

If you can, use instead SSH keys per environment, as I illustrate there: then you can easily maintain different identities for the same remote repo (github.com)


Note: the GCM (Git Credential Manager) installed alongside Git for Windows does not, as stated in issue 363, support multiple users per Uri.

GCM 2.2.0 (July 2023) adds login, logout and list commands for the GitHub provider, as well as UI and TTY prompts to select between GitHub user account.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yes, the windows credential manager stores the GitHub credentials but seems to be only able to handle credentials for one account. Me trying to manually edit and add credentials didn't work. I'm now using your answer using ssh, even though it was some overhead setting it up and adjusting the remote urls for all local repositories. So if there is another, easier answer, that's still welcome. – stefanbschneider Jan 28 '18 at 09:40
  • @CGFoX Yes, I have completed the answer to point to the exact manager used, and the fact it does not yet support multiple users per URI. – VonC Jan 28 '18 at 11:36
  • Since there doesn't seem to be a proper answer using https and your ssh solution is reasonably simple, you'll get the bounty ;) – stefanbschneider Feb 01 '18 at 12:29
1

I just setup a mutli-credential setup for my Jenkins that might be applicable to what you're doing.

For our Jenkins user, we're pushing our configuration to AWS CodeCommit to backup the server. We also need the ability to use the mvn release plugin which requires the ability to push to our Github repo. Our jenkins instance is also in a subnet that prevents outgoing SSH so we have to use HTTPS.

Thus, we need two sets credentials. It should also be noted that our Github organizaiton requires MFA so the password is actually the personal access token.

# /var/lib/jenkins/.gitconfig
[user]
  name = Jenkins
  email = jenkins@domain.io
[credential]
  helper = !aws --profile default codecommit credential-helper $@
  UseHttpPath = true
[credential "https://github.com"]
  helper = store

# /var/lib/jenkins/.git-credentials
https://username:password@github.com/SomeOrg/some-repo

One additional point is that since it seems both your repos/organizations are on Github, here are some additional points from the git documentation that might be applicable to you:

useHttpPath - By default, Git does not consider the "path" component of an http URL to be worth matching via external helpers. This means that a credential stored for https://example.com/foo.git will also be used for https://example.com/bar.git. If you do want to distinguish these cases, set this option to true.

https://git-scm.com/docs/gitcredentials

chizou
  • 1,262
  • 13
  • 23
0

Do this, though lengthy works fine

  • Delete github.com credentials from windows credentials(search windows credentials on PC).
  • Set credentials for the target repository using git config user.name 'username' and git config user.email 'email' in the working directory.
  • Most atimes, you have to make sure you are logged out of your unwanted repository and logged into the target account because during the process of pushing you will be prompted to sign in which usually uses the credentials of the currently signed-in git account.
  • Now push! considering the previous point above
-3

You can set a user for that repository only by typing:

git config --local user.name 'Full Name'

git config --local user.email 'your@mail.com'

This wont affect you other repositories.