0

According to this SO question (here) and many others, you can change the user you are working with by simply

git config user.name "username1"
git config user.password "password1"

Ok. Great that's done, and when I check with

git config user.name //I get username1

But as soon as I push to my git repo that I created in the account using that username, I get...

remote: Permission to username1/reponame.git denied to username2

I should mention, when I type

ssh -T git@github.com

I get

Hi username2! You've successfully authenticated, but GitHub does not provide shell access.

So, ssh authentication takes over any other way of signing into git? How can I maintain that ssh authentication with username2 for the username2/repo.git (work github account), while still being able to switch to username1/repo.git (personal github account)?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
  • 2
    Setting `user.name` etc does not set anything about your user. It sets the text string that goes into a new `git commit` you make. That text string is later displayed as "the author of the commit", which is not a user, it's just a text string. *Authentication* is hard and Git doesn't do that at all: it fobs it all off on other programs or libraries. – torek Feb 03 '22 at 14:44
  • 2
    Meanwhile, *nothing uses `user.password`*. You can set it to anything you like because nothing uses it. You can set `user.eiffeltower` to `london` if you like, with the same (i.e., no) effect. – torek Feb 03 '22 at 14:45
  • 2
    I've explained, in great detail, how ssh works in [numerous](https://stackoverflow.com/q/37518346/1256452) [other](https://stackoverflow.com/q/69919549/1256452) [questions](https://stackoverflow.com/q/68490014/1256452); see these selected ones for various details. – torek Feb 03 '22 at 14:52
  • 2
    (I'm really curious as to why people keep saying to set `user.password`. There must have been *some* software that used it, at some time, but I have never found out what it was!) – torek Feb 03 '22 at 14:53
  • @torek No idea man. Someone requests it as a solution and I guess it sounds like an intelligent answer because whereever there is a username, a password is expected. But I get what you're saying. I'll go see your other posts and comment there if I have any questions. Please keep an eye on the notifications as I would like to resolve this isssue that's road-blocking me. Thanks – LOTUSMS Feb 03 '22 at 14:56
  • Looks like you are trying to do push to repository you do not have access to. You should clone repository on github to your account and then use url of new repo form your account on your local machine to have possibility to do pushes. – Marek R Feb 07 '22 at 12:40
  • 1
    Does this answer your question? [Multiple GitHub accounts on the same computer?](https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer) – AD7six Feb 07 '22 at 13:38
  • 1
    Oh, you found it. Voting to close then. – Andrejs Cainikovs Feb 07 '22 at 13:39

1 Answers1

1

You are trying to configure git, but the truth is that you need to configure ssh for two different users. Your ssh does not know nothing about your git configuration - you need to prepare your .ssh/config in such a way so that you could work with GitHub (in your case) from two different accounts.

This is how I do it:

Host github.com
        User git
        IdentityFile ~/.ssh/id_ed25519-account1
Host github-account2
        User git
        IdentityFile ~/.ssh/id_ed25519-account2

Here, whenever I need to work with account1 on GitHub - it will just work. However, if I need to work from my another account - I change remotes by replacing github.com part to github-account2, like this:

git clone git@github-account2:torvalds/linux.git

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95