Using multiple githib accounts from the same computer

I have two GitHub accounts, one for professional use, and one for personal. I have created different Personal Access Tokens for each one.

I have repos at different locations on my hard drive, ex:

I am finding that, if I go to “C:\professional\repo-dir” and enter the command “git push origin master”, I am seeing the update I expect in my professional github account, but the update was performed by my personal account.

My questions are:

  1. What could be causing this behavior?
  2. What do I need to do to ensure that each action is performed by the correct user?

What exactly do you mean by this? Are the commits ascribed to the wrong user, or something else?

That is exactly it. The commits are ascribed to my personal account user, even though I am pushing to my professional account repository.

I am not asked for any sort of credentials, token, username+password, etc. at the time of the commit or push.

Who is pushing and who the commits are ascribed to is independent of each other. From what you’re describing it looks like the push is authorized with credentials for your professional account, but the author email address in the commits is one associated with your private account.

The author email address to use is commonly set globally (for your whole user account on the system), but you can also set it in each repository. You’ll need to do that if you want to use different addresses on a per-repository basis (repositories without the settings will still use the globally configured one).

If you want to fix the existing commits you’ll have to rewrite the commit history after setting the correct email address.

In that case:

  1. How do I set the email address for a repository?
  2. If I set the email address for a repository, will the credentials associated with that email address automatically be used for commits and pushes to that repository?

See Setting your email address for a single repository. The only difference to setting it for every repository is the absence of the --global flag, git config commands apply to the current repository only by default.

No, that’s completely independent: If you’re authorized to push to a repository, you can push commits authored by anyone. You’ll have to set the right credentials to use, how to do that depends on the access method you use (token for HTTPS, key for SSH).

I am currently using the HTTPS method, with a Personal Access Token. As mentioned above, I created a token for each of my github accounts, but I am not being asked for a token when I do a commit or push.

Ideally, I would like things to work so that it would determine which account/token to use based either on the location in the filesystem (ex: C:\professional\repo-dir or C:\personal\repo-dir) or by the repo I’m interacting with. Is this possible?

That means your username and token are cached or stored in some kind of credential manager. Exactly how to find that depends on your platform. Based on your path examples that seems to be Windows, which I’m not familiar with.

Per repo should be possible by setting the username, check the Git docs on the credential.<url>.* config options, and Git - gitcredentials Documentation. I assume the credential manager will be smart enough to ask for separate passwords for different usernames. :wink: