title: Multiple Github Accounts With Git In Windows
body: <p>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.</p>
  
  <p>Basically it went like this,</p>
  
  <ul>
  <li>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)</li>
  <li>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.</li>
  <li>Clone repo using corresponding Host entry in ssh config for original
  account git clone :/.git</li>
  <li>Attempt to push changes to repo and is successful Clone repo using
  corresponding Host entry in ssh config for second account git clone
  &lt;2nd Host>:&lt;2nd username>/.git</li>
  <li><p>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.</p></li>
  <li><p>Clearing the git entries in the windows credential manager did not
  resolve this issue.</p></li>
  <li><p>Clearing the global user name and email did not resolve this issue</p></li>
  </ul>
  
  <p>I was finally able to push my changes using the following:</p>
  
  <pre><code>GIT_SSH_COMMAND="ssh -i &lt;path to private ssh key for second user&gt;" git push
  </code></pre>
  
  <p>I am posting this both for others who have experienced this issue and also to ask a few questions,</p>
  
  <ol>
  <li><p>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?</p></li>
  <li><p>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? </p></li>
  </ol>
  
  <p>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.</p>
  
  <hr>
  
  <h3>HTTP Paths</h3>
  
  <p><a href="https://github.com/schwaggs/testssh" rel="nofollow noreferrer">https://github.com/schwaggs/testssh</a></p>
  
  <p><a href="https://github.com/jjschweigert/testrepo" rel="nofollow noreferrer">https://github.com/jjschweigert/testrepo</a></p>
  
  <h3>SSH Paths</h3>
  
  <p>git@github.com:schwaggs/testssh.git</p>
  
  <p>git@github.com:jjschweigert/testrepo.git</p>
  
  <h3>SSH Config File</h3>
  
  <pre><code>$ 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
  </code></pre>
  
  <h3>Cloning With Original Account</h3>
  
  <pre><code>$ 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.
  </code></pre>
  
  <h3>Pushing To Original Account (jjschweigert)</h3>
  
  <pre><code>$ 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 -&gt; master
  </code></pre>
  
  <h3>Cloning From Second Account (schwaggs)</h3>
  
  <pre><code>$ 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.
  </code></pre>
  
  <h3>Pushing To Secondary Account</h3>
  
  <pre><code>$ 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.
  </code></pre>
  
  <h3>SSH -T Outputs</h3>
  
  <pre><code>$ 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.
  </code></pre>