2

I'm trying to set up personal git account along with my work account. I followed this guide https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/ and my ~/.ssh/config is (some-name is my user name for personal github account)

host github.com
   Hostname github.com
   User git
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/id_rsa
host some-name.github.com
   Hostname github.com
   Identityfile ~/.ssh/id_rsa_personal

After this, I ran ssh-add ~/.ssh/id_rsa_personal which was successful.

However, when I do ssh -T some-name.github.com, it fails with somename@github.com: Permission denied (publickey) The work account is working fine. I also uploaded newly generated public key on github account.

My git config --list shows user.name=some-name.

update: based on VonC's response, I was able to get ssh -T some-name.github.com to run successfully. However, I am still getting following error when I try to push code into a repo in github account.

ERROR: Permission to some-name/docker-react.git denied to User.
fatal: Could not read from remote repository.

I checked the ssh keys uploaded on github, and it is correct.

uzumas
  • 632
  • 1
  • 8
  • 23

4 Answers4

5

First, add User git to your second config:

host some-name.github.com
   Hostname github.com
   Identityfile ~/.ssh/id_rsa_personal
   User git

The OP agyeya had to then modify the remote origin URL accordingly:

git remote set-url origin git@some-name.github.com:some-name/docker-react.git

If this is still not working (assuming the public key was registered to your GitHub account):

Try and create first keys without associated passphrase, to test it out.
That will avoid the ssh-agent need (again, for testing)

See "what is the correct format for private key in Credentials", to create one with the old PEM format:

ssh-keygen -m PEM -t rsa -P "" -f ~/.ssh/id_rsa_personal
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @agyeya You had selected the right answer before. What happened? – VonC Jan 07 '20 at 21:41
  • @agyeya Is "some-name" your own GitHub account? Or are you declared as a collaborator in the repo `some-name/docker-react` – VonC Jan 07 '20 at 21:43
  • "some-name" is my own github account. One thing I noticed- it's using somename@github.com. Is this userid generated internally by github using my name? – uzumas Jan 08 '20 at 01:15
  • I essentially have 2 id_rsa(_personal) files. Could it be that wrong one is being picked? Although I have explicitly done ```ssh-add ~/.ssh/id_rsa_personal``` , not sure if this getting picked during git push. – uzumas Jan 08 '20 at 01:24
  • Was able to solve git push with this: ```git remote set-url origin git@some-name.github.com:some-name/docker-react.git ``` What happened was the remote url alrerady had some-name in it, and so I missed it while setting remote earlier. I ended up using ```git remote set-url origin git@github.com:some-name/docker-react.git``` – uzumas Jan 08 '20 at 01:28
  • @agyeya Well done. I have included your comment in the answer for more visibility. – VonC Jan 08 '20 at 07:12
  • Thanks for the help VonC. However, I guess I framed the sentence incorrectly- What works is ```git remote set-url origin git@some-name.github.com:some-name/docker-react.git``` Please update this in your answer so that no one finds incorrect info – uzumas Jan 08 '20 at 10:15
0

Did you upload your public key to github? In github you can check which public keys are uploaded, see if you uploaded it and that you uploaded the correct one.

0

You can define key at a repository level.

Configuration core.sshCommand: From Git version 2.10.0, you can configure this per repo or globally, so you don't have to set the environment variable any more!

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
git pull
git push

This will add a key entry in the .git/config file. So whenever you perform any git actions this will use the defined ssh key.

Environment variable GIT_SSH_COMMAND: From Git version 2.3.0, you can use the environment variable GIT_SSH_COMMAND like this:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example

Note that -i can sometimes be overridden by your config file, in which case, you should give SSH an empty config file, like this:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example
Chetan Patel
  • 772
  • 4
  • 8
0
  1. Go to command in your computer, type "Manage Windows Credential " step1
  1. Remove your history github records. step2

3. commit your code git bash again and then it works