GitHub: Separate credentials for two accounts on Windows

陌路散爱 提交于 2019-11-28 11:22:41

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!