3

So this is a very specific use case. It would be great if any GitHub gurus can help me.

In my Linux laptop, I want to push to same GitHub repo using two different GitHub usernames.

I have setup both the SSH keys in my local machine. I have been able to configure it up until being able to switch between users when committing. This works fine and I can switch between the two configured users while committing.

However whenever I do git push I am never able to switch users and it always uses my first username.

Is there a way I can switch or select user when I push to github?

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69
  • `I have setup both the SSH keys in my local machine` have you set the certificates up on the local machine *or the same account?* on that machine? If you used different accounts you wouldn't have any problems. Right now you have two different certificates installed for the same remote server on the same account. – Panagiotis Kanavos Nov 24 '20 at 15:30
  • I do not know how to set up the certificates for different account, I guess. If you know, will you write that as an answer,@PanagiotisKanavos – Undefined Variable Nov 24 '20 at 15:43
  • 1
    Does this answer your question? [Specify an SSH key for git push for a given domain](https://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-for-a-given-domain) – Nils Werner Nov 24 '20 at 15:43
  • @NilsWerner this link looks useful, I will check it out, thank you! – Undefined Variable Nov 24 '20 at 15:47

2 Answers2

1

paraphrasing this answer :

You can create two different hostnames in your ~/.ssh/config.
For example, if your configuration looks like this:

Host github-as-alice
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/id_ed25519.alice
  IdentitiesOnly yes

Host github-as-bob
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/id_dsa.bob
  IdentitiesOnly yes

Then you just use github-as-alice and github-as-bob instead of the hostname in your URL:

git clone github-as-alice:name/whatever.git
cd whatever
git remote add bob github-as-bob:name/whatever.git

note: you can also have a "by default" key for github by simply mentioning it in a Host github.com section :

Host github.com
  User git
  IdentityFile /home/whoever/.ssh/id_ed25519.github
LeGEC
  • 46,477
  • 5
  • 57
  • 104
-1

If all you want to do is use a different username, then you could set the username in the config right before you push each time:

$ git config user.name "other_username"
$ git config user.email "other_username"
$ git push

Notice that git uses your username when you commit, so you would have to change the username before you commit and then push as this user.

See this answer.

Shell Code
  • 682
  • 4
  • 9