{"id":380539,"date":"2021-12-13T22:00:50","date_gmt":"2021-12-13T19:00:50","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it\/"},"modified":"2021-12-13T22:00:50","modified_gmt":"2021-12-13T19:00:50","slug":"how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it\/","title":{"rendered":"#How to Fix Git Using the Wrong SSH Key &#038; Account \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a33b14259654\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a33b14259654\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it\/#Whats_The_Problem\" >What\u2019s The Problem?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it\/#Making_Sure_Git_Is_Configured_Properly\" >Making Sure Git Is Configured Properly<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it\/#Editing_sshconfig\" >Editing\u00a0~\/.ssh\/config<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-fix-git-using-the-wrong-ssh-key-account-cloudsavvy-it\/#Troubleshooting\" >Troubleshooting<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Fix Git Using the Wrong SSH Key &amp; Account \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-14290\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When connecting to a remote Git server, like Github, your client must authenticate using HTTPS or SSH based tokens. If you\u2019re using the latter, you may run into issues with key handling, including Git using the wrong SSH key and signing you into a different account.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Whats_The_Problem\"><\/span>What\u2019s The Problem?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you don\u2019t have an SSH key at all on your account, you\u2019ll almost certainly get the following error when trying to push:<\/p>\n<pre>Permission denied (publickey).&#13;\nfatal: Could not read from remote repository.<\/pre>\n<p>This is probably the most common Git mistake, and the solution to this is simple\u2014set up an SSH key for your account.<\/p>\n<p>But, you can also run into a very similar looking error message, even after having an SSH key set up properly. If you use multiple Git accounts, you may have this error:<\/p>\n<pre>Permission to Username\/Repository.git denied to otheraccount.&#13;\nfatal: Could not read from remote repository.<\/pre>\n<p>In this case, Github has accepted your SSH key, but it\u2019s still not working correctly; even though your local\u00a0<code>git<\/code> repo is configured to use the right email address, if you give it the wrong key, it will authenticate you as the wrong user. This is fundamentally different from the first error. It is not \u201cpermission denied,\u201d but instead \u201cpermission denied to account.\u201d<\/p>\n<p>This can be a problem if you have multiple accounts and need to use separate keys for each of them. Due to the way SSH authentication works, it will still \u201csign you in\u201d to Git\u2019s remote server, but as the wrong user. When Github checks if that user has access to the repository, it will likely fail, thus producing the error saying your account does not have permission to use your own repo.<\/p>\n<p>The solution is to properly manage multiple SSH keys, so that your <code>git<\/code>\u00a0client doesn\u2019t send the wrong key to the server. You can do this by editing SSH\u2019s configuration files.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Making_Sure_Git_Is_Configured_Properly\"><\/span>Making Sure Git Is Configured Properly<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First off, you\u2019ll want to make sure your repo is configured to use the right email address and username. You can check that from the Git config:<\/p>\n<pre>git config --list<\/pre>\n<p>And set the name and email for this repo (or using <code>--global<\/code>\u00a0to set it for all repos):<\/p>\n<pre>git config user.name \"Name\"&#13;\ngit config user.email \"email@gmail.com\"<\/pre>\n<p>If you\u2019re not having SSH key issues, this may be all that is required.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Editing_sshconfig\"><\/span>Editing\u00a0~\/.ssh\/config<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you don\u2019t have an existing key you\u2019d like to use already, you will need to generate a new key file for your account. If you do, you\u2019ll need to move or rename the proper one into <code>~\/.ssh<\/code>\u00a0so it does not conflict with your default <code>id_rsa<\/code>. In this case, we\u2019ll create a new key named \u201cgithub\u201d:<\/p>\n<p>ssh-keygen -t rsa -f ~\/.ssh\/github<\/p>\n<p>To use multiple SSH keys, you will need to edit <code>~\/.ssh\/config<\/code>\u00a0and set up a new Host block for Github that points to the keyfile you just created. SSH will use this to select the SSH key it will use.<\/p>\n<pre>Host main&#13;\n  Hostname github.com&#13;\n  IdentityFile ~\/.ssh\/github&#13;\n  IdentitiesOnly yes&#13;\n&#13;\nHost old&#13;\n  Hostname github.com&#13;\n  IdentityFile ~\/.ssh\/id_rsa&#13;\n  IdentitiesOnly yes<\/pre>\n<p>If you only have the one host block for the new key, you shouldn\u2019t have to do anything else, and will be able to connect to Github now. If you have multiple blocks for different keys, you\u2019ll need to edit your repository\u2019s remote so that <code>github.com<\/code>\u00a0is replaced by the name of the host block in SSH\u2019s config file. This will manually select a key rather than relying on a conflicting lookup.<\/p>\n<pre>git remote remove origin&#13;\n&#13;\ngit remote add origin git@main:username\/repository.git<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Troubleshooting\"><\/span>Troubleshooting<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>After setting up SSH host configuration and configuring your remotes, Git should use the right SSH key when pushing. If you\u2019re still having problems, you can debug the issue by manually setting the SSH command that Git uses for a shell session using the <code>GIT_SSH_COMMAND<\/code>\u00a0environment variable.<\/p>\n<pre>GIT_SSH_COMMAND='ssh -i ~\/.ssh\/github -o UserKnownHostsFile=\/dev\/null -o StrictHostKeyChecking=no'<\/pre>\n<p>You\u2019ll also want to check and make sure Git is using the key from the place you\u2019re expecting. On Windows, this location can vary wildly depending on what software you\u2019re using Git through. For example, using the GUI client Fork, it defaulted to the Windows SSH agent, but I had configured a new SSH key which it was using instead.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14918\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/4f20fa77.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"751\" height=\"322\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14914\/how-to-fix-git-using-the-wrong-ssh-key-account\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Fix Git Using the Wrong SSH Key &amp; Account \u2013 CloudSavvy IT&#8221; When connecting to a remote Git server, like Github, your client must authenticate using HTTPS or SSH based tokens. If you\u2019re using the latter, you may run into issues with key handling, including Git using the wrong SSH key and signing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":380540,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-380539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/380539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=380539"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/380539\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/380540"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=380539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=380539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=380539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}