2

I set up two github accounts using ssh keypairs. Something which might become annoying is the fact that I have to set up user.name and user.email for each repo. I want something like this:

git switch {account}

I don't think git provides anything like this, so I thought of making a .bat/.sh script to switch between the accounts. How could I open a file (like ~/account1) and read data from it (in .bat / .sh)

robert
  • 731
  • 1
  • 5
  • 22

1 Answers1

2

The user.name/user.email has nothing to do with the account used to push to GitHub.

They only impact how the commit author is shown on GitHub

And that configuration can be set in each repository anyway, which means you don't have to change it when switching repos.

 cd /path/to/repo1
 git config user.name account1
 git config user.email account2

The public key registered to a GitHub account is the one authenticating you.

Your ~/.ssh/config file can reference the right key for the right repo, as described in "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?"

You will need to change the remote repo URL accordingly:

cd /path/to/local/repo
git remote set-url origin github1:account1/repoA

With "github1" being a Host name section referencing the right public ssh key.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That's what I thought too. When pushing on account2 (I've used account1 with https before, and therefore thats the global user.name and user.email) I would authenticate, enter keyphrase, but still push with account1. Only after changing user.name and user.email would I push form account2. – robert Dec 16 '18 at 00:50
  • @robert no need to use ssh private key with passphrase in your case. And again, user.name/email have nothing to do with the account used to push. – VonC Dec 16 '18 at 00:52
  • How did I then push with an account that was linked to a completely different ssh key? I pushed, it asked me for my keyphrase for id_rsa_account2, but still pushed with account1 – robert Dec 16 '18 at 00:54
  • @robert Was your remote URL (displayed when done a git remote -v) actually an SSH one? or an HTTPS one? – VonC Dec 16 '18 at 00:54
  • @robert And when you do a `git config -l --show-origin`, do you see any `insteadOf` directive? – VonC Dec 16 '18 at 00:55
  • My remote was github-account2:account2/repo.git / No insteadOf. Like I said though, this issue went away after I set name/email – robert Dec 16 '18 at 00:56
  • @robert That means you are using a ~/.ssh/config already, which should reference the account2 ssh key, registered to the account2 GItHub account (irrespective of any user.name/email, which have nothing to do regarding the push, but impact only how the author is represented on each commit) – VonC Dec 16 '18 at 00:57