6

I recently ran into an issue where I could not push changes into a repository I had cloned down as another user from the first user I pushed with in git on my desktop.

Basically it went like this,

  • Use git for the first time which asks for github credentials when pushing to a repository. These credentials are then used for all pushes regardless of how the repo was cloned (which ssh key, user, etc)
  • Generate SSH keys for both github accounts and add entries to the ssh config to target these identity files. Keys are added to each github account as well.
  • Clone repo using corresponding Host entry in ssh config for original account git clone :/.git
  • Attempt to push changes to repo and is successful Clone repo using corresponding Host entry in ssh config for second account git clone <2nd Host>:<2nd username>/.git
  • Attempt to push changes to repo and receive error that the original username does not have permission, even though this was cloned using the second user and more specifically an ssh key.

  • Clearing the git entries in the windows credential manager did not resolve this issue.

  • Clearing the global user name and email did not resolve this issue

I was finally able to push my changes using the following:

GIT_SSH_COMMAND="ssh -i <path to private ssh key for second user>" git push

I am posting this both for others who have experienced this issue and also to ask a few questions,

  1. I understand this command is essentially specifying the key for the ssh connection to use when it makes it's push, but why is this key not already targeted if it was cloned using that same identity file?

  2. Are there any alternatives to this or better approaches that are not tedious work like manually changing config values or removing entries from the windows credential manager?

So the goal would be to push changes to multiple github accounts without having to do things like temporarily specify the ssh key to use.


HTTP Paths

https://github.com/schwaggs/testssh

https://github.com/jjschweigert/testrepo

SSH Paths

git@github.com:schwaggs/testssh.git

git@github.com:jjschweigert/testrepo.git

SSH Config File

$ cat ~/.ssh/config
Host jjschweigert
 HostName github.com
 User git
 IdentityFile ~/.ssh/jjschweigert_key
Host schwaggs
 HostName github.com
 User git
 IdentityFile ~/.ssh/jjschweigert_key

Cloning With Original Account

$ git clone jjschweigert:jjschweigert/testrepo.git
Cloning into 'testrepo'...
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 28 (delta 0), reused 28 (delta 0), pack-reused 0
Receiving objects: 100% (28/28), done.

Pushing To Original Account (jjschweigert)

$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 261 bytes | 43.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To jjschweigert:jjschweigert/testrepo.git
   c082e38..31b7830  master -> master

Cloning From Second Account (schwaggs)

$ git clone schwaggs:schwaggs/testssh.git
Cloning into 'testssh'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 21 (delta 0), reused 18 (delta 0), pack-reused 0
Receiving objects: 100% (21/21), done.

Pushing To Secondary Account

$ git push
ERROR: Permission to schwaggs/testssh.git denied to jjschweigert.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

SSH -T Outputs

$ ssh -T jjschweigert
Hi jjschweigert! You've successfully authenticated, but GitHub does not provide shell access.


$ ssh -T schwaggs
Hi jjschweigert! You've successfully authenticated, but GitHub does not provide shell access.
  • I can answer this part: *why is this key not already targeted if it was cloned using that same identity file?* Because `git` doesn't store the key used to clone. You have to configure it yourself either by setting `$GIT_SSH_COMMAND` environment variable or by configuring [`git config core.sshCommand "ssh -i "`](https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresshCommand) or by manipulating with host names. – phd Mar 21 '19 at 11:06
  • phd, it ended up being a wrong identity file path for my second user in the ssh config file. Thanks for your input! –  Mar 21 '19 at 12:45
  • Just wanted to add this article for future viewers who are using two github accounts on their local machine. This helped me a lot [link](https://www.cloudsavvyit.com/2245/how-to-manage-multiple-git-accounts-on-one-system/). – dko Jan 10 '22 at 00:09

3 Answers3

5

Clearing the git entries in the windows credential manager did not resolve this issue.

If you are using ssh URL (git@github.com:<user>/<repo>), the credential manager is not involved: it provides credentials for https:// URLS only.

Use git for the first time which asks for github credentials when pushing to a repository.

That would be the case only if the remote origin URL is an https one.

So the goal would be to push changes to multiple github accounts without having to do things like temporarily specify the ssh key to use.

That is done through an ssh config file: see as practical examples:


From the edit

Host user1
 HostName github.com
 User git
 IdentityFile ~/.ssh/iser1_key  <<====
Host user2
 HostName github.com
 User git
 IdentityFile ~/.ssh/user1_key  <<==== same key!? Meaning same user!

you cannot expect push as user2, if the SSH config for user2 entry refers to user1 private key.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks VonC for the insight, –  Mar 21 '19 at 05:47
  • Thanks VonC for the insight, as described above I am using multiple ssh keys and I did add the entries in the ssh config. When cloning using these it works fine. But when pushing as the second user it does not work unless I use the command mentioned previously. I also asked two questions regarding using the config file entries. Also do you mean if I do not clone any repos using https I should never have default credentials git tries to use when pushing? –  Mar 21 '19 at 05:53
  • "But when pushing as the second user it does not work unless I use the command mentioned previously. " : it will work if you use the proper SSH URL, the one refererring to the correct enty in the ssh config. Without seing the exact URL/syntax used fir the remote origin URL, I cannot determine why it fails to use the second user SSH key. – VonC Mar 21 '19 at 05:55
  • @Schwagmister "if I do not clone any repos using https I should never have default credentials git tries to use when pushing?": that would means using SSH, and credentials are not needed in that case: the private SSH key is enough to identify you. – VonC Mar 21 '19 at 05:56
  • It is using the exact same format as the original user yet I cannot push unless I add that command in to re-specify the identity file to use even though it was cloned using the same identity file. I can provide exact commands when I am at my desktop and any outputs you need. –  Mar 21 '19 at 05:57
  • @Schwagmister That would be helpful: I cannot interpret "It is using the exact same format as the original user" – VonC Mar 21 '19 at 05:58
  • So in general terms I have two entries in the ssh config both taking the form Host [new hostname] HostName github.com user git IdentityFile [path to private key]. From there I clone the repos using these entries, git clone [new hostname]:[username]/[repo].git. I make some changes and then in each respective repo when I try to do git push I am able to push in the repo cloned from the original account, but in the repo cloned from the second user I get a message saying that the original user doesn't have permissions. This means it is trying to push as the original user I used in Git right? –  Mar 21 '19 at 06:05
  • The second repo was cloned down using the identity file from the respective entry in the ssh config yet when I push it is trying to use the original user rather than the identity file it was cloned with –  Mar 21 '19 at 06:06
  • @Schwagmister not necessarily : try `ssh -T SecondNewHostname`: GitHub will tell you who you are. – VonC Mar 21 '19 at 06:07
  • I tried that and I don't remember the username it gave but it was successfully authenticated, I swear it was the correct user. But where it's failing is during pushing. I mean if I check the .git/config of that repo it is using the correct path i.e. [new hostname]:[username]/[repo].git which should be using that same identity file it cloned with yet the message states it is trying to authenticate with the other user. –  Mar 21 '19 at 06:10
  • @Schwagmister "I tried that and I don't remember the username it gave but it was successfully authenticated,": can you try it again? – VonC Mar 21 '19 at 06:11
  • @Schwagmister Again, without seeing specific syntax used, I cannot be sure of what is going on here. – VonC Mar 21 '19 at 06:11
  • Just updated my question with everything Von, let me know if you need more. –  Mar 21 '19 at 06:29
  • Von, that's it, that stupid identity file. I copied the first entry and forgot to change the path. Thanks for your help! –  Mar 21 '19 at 12:41
  • @Schwagmister No problem. I certainly have my fair share of copy-paste error ;) – VonC Mar 21 '19 at 12:41
  • Perhaps I rushed and didn't triple check! Anyway I really appreciate the help! –  Mar 21 '19 at 12:43
4

Per the conversation with VonC you can clearly see in the ssh config file the identity file for the second user was incorrect as it pointed to the first users file. The second entry was copied from the first and this value was not changed.

After modifying the value to point to the correct key i.e. ~/.ssh/schwaggs_key I could clone and push without issue. As a side note I have to set the user's email and name properties in git for each repository pulled from each user i.e once inside the repo,

git config user.email "github account email"

git config user.name "github account username"
0

What you need to do is to create a new ssh key for your second Github account because by default git is using the key from your first Github account and when you try to push from the second account you will get remote: Permission to privateuser/privaterepo.git denied to workuser. workeruser in this case is your first Github account's username. So you cant push to your second Github account using the first Github account's username. Follow this link for help.

https://www.youtube.com/watch?v=fnSRBRiQIU8

  • Your answer is good, but it could be improved by linking to the official github docs as well, and not just a youtube video. – 00prometheus Jun 03 '20 at 12:23