by gigel82 · 5y ago · 0 comments
I want to have work and personal GitHub accounts on the same machine and very explicitly choose which account goes to which repo. Too often I have changes going in with the wrong user.name / user.email or account to the point where I paranoidly reauthenticate every time and manually check each clone's .git/config.
Host github-personal
HostName github.com
IdentityFile ~/.ssh/id_rsa_personal
Host github-work
HostName github.com
IdentityFile ~/.ssh/id_rsa_work
Then you can use `github-work` or `github-personal` in the remote URL like `git clone git@github-work:mywork/somerepo.git`.edit: I realized after reading the other comments that I got the problem wrong! This would push the commits from the correct GitHub account but the commits would still have the e-mail from git's config and GitHub would link the account in the committer e-mail.
That seems like a situation where you'd `git config user.email` within your work repo(s) so that it's set locally, not globally. I also think GitHub primarily supports and envisions people using one GH account for work and personal projects.
Edit: but also, GCM Core has nothing to do with the user.email/user.name in .gitconfig. I have a similar problem where I want some commits written with my work email and others with my personal, and I always forget to check it when starting in a new repo. I push them both using a single GitHub identity, though, so your situation has an extra wrinkle.
git clone https://vtbassmatt@github.com/WorkOrg/work-repo.git
I manually added the "vtbassmatt@" to tell Git to tell GCM Core that that's who I want to log in as.(Here's where I could tell you about all the ways in which the credential manager system in Git is deficient... I'll spare you, though, and simply say that this is close to the best we can do without a whole other layer of artifice that you probably wouldn't like.)
https://alice@github.com/owner/repo
GCM will store credentials for "alice" only. Repositories that have remote URLs with a different "user@" will use different credentials.
Remote URLs without a "user@" part at all (like "https://github.com/owner/repo") are treated like the "default" user for GitHub.com, rather than a specific one.
In my ~/.gitconfig file, I've included
[credential "https://github.com"]
useHttpPath = true
Which means that the first time -- and only the first time -- I try to do something with a GitHub repo that requires authentication, I'll be asked for the username and password (token), and I make sure to use the right one. :) At that point things stay set.Went to bed early rather than finish that, and this just reminded me of it... (offhand, I wouldn't be surprised if my alias has to be something other than `clone`, but no biggie if so)
You can configure it to set your git user name/email based on your working directory, for example. This is how I keep the separation you mentioned between personal and work on the same machine
Also, it doesn't seem to solve the problem of multiple accounts; I more or less accepted I need to manually configure user.name and user.email for each clone, but the account is not part of the same config, it looks like it's stored in some other ephemeral place - maybe GCM Core, or some Credential Manager or who knows.
I only realize it was using the wrong account when I try to push and get some unauthorized error; by that point, my commit log is polluted with the wrong account so I need to do some gymnastics where I reset the authentication, manually copy over files or unstage to purge the metadata, etc.