{"id":368405,"date":"2021-11-17T15:30:33","date_gmt":"2021-11-17T12:30:33","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-switch-a-github-repository-to-ssh-authentication-cloudsavvy-it\/"},"modified":"2021-11-17T15:30:33","modified_gmt":"2021-11-17T12:30:33","slug":"how-to-switch-a-github-repository-to-ssh-authentication-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-switch-a-github-repository-to-ssh-authentication-cloudsavvy-it\/","title":{"rendered":"#How To Switch a Github Repository to SSH Authentication \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a27e24523a3d\" 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-6a27e24523a3d\" 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-switch-a-github-repository-to-ssh-authentication-cloudsavvy-it\/#Moving_To_SSH_Authentication\" >Moving To SSH Authentication<\/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-switch-a-github-repository-to-ssh-authentication-cloudsavvy-it\/#Swapping_an_HTTPS_Repo_To_SSH_Authentication\" >Swapping an HTTPS Repo To SSH Authentication<\/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-switch-a-github-repository-to-ssh-authentication-cloudsavvy-it\/#Using_a_Different_SSH_Key\" >Using a Different SSH Key<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To Switch a Github Repository to SSH Authentication \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-7125\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Github provides two ways to authenticate with your repository\u2014over HTTPS with a password, and over SSH using private keys. While both work fine, if you want to switch, you\u2019ll need to configure your local repo to use the new credentials.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Moving_To_SSH_Authentication\"><\/span>Moving To SSH Authentication<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Github defaults to HTTPS authentication, using the password for your Github account. Whenever you go to clone a repo, you\u2019ll have to manually select \u201cSSH\u201d as the option for the clone URL if you prefer using that.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14748\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/5a35cfc6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"472\" height=\"188\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><br \/>\nWhile SSH is often best for authenticating connections to Linux servers, Github recommends HTTPS because it\u2019s easy for people and causes less confusion. However, it\u2019s worse for automation, not technically as secure as an RSA key, and can be an annoyance typing it out each time, even with credential caching.<\/p>\n<p>To switch over is pretty simple\u2014you will need to create a new SSH key if you don\u2019t have one, add it to your account, and then swap your local repo over to the new endpoint if you\u2019ve already cloned.<\/p>\n<p>First, check if you have an SSH key already. Your default one is usually stored here on Linux\/macOS:<\/p>\n<pre>cat ~\/.ssh\/id_rsa.pub<\/pre>\n<p>On Windows, it depends on the program you\u2019re using. It\u2019s sometimes stored in <code>%HOMEDRIVE%%HOMEPATH%.ssh<\/code>, but may be different based on what you\u2019re using Git from. In most cases, we recommend using Windows Subsystem For Linux (WSL) which works like a VM and stores the key in a traditional Linux environment.<\/p>\n<p>If you don\u2019t have one, you can make one with <code>ssh-keygen<\/code>:<\/p>\n<pre>ssh-keygen -t rsa -f ~\/.ssh\/id_rsa<\/pre>\n<p>Once you have the key, head over to your Github user settings under \u201cSSH and GPG Keys,\u201d and paste in the contents of <code>id_rsa.pub<\/code>\u00a0into a new key.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14749\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/7ec33b2b.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1927\" height=\"1338\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once done, you should be authenticated, provided Git is set up to use this key.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Swapping_an_HTTPS_Repo_To_SSH_Authentication\"><\/span>Swapping an HTTPS Repo To SSH Authentication<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you cloned from Github using HTTPS, your repository will already be linked to Github using that remote URL. To fix this, you\u2019ll need to remove the HTTPS remote, usually called <code>origin<\/code>, and add it back with the proper <code>git@github<\/code>\u00a0URI that uses SSH.<\/p>\n<pre>git remote rm origin&#13;\ngit remote add origin git@github.com:user\/repo.git<\/pre>\n<p>Then push to origin as normal:<\/p>\n<pre>git fetch origin&#13;\ngit push --set-upstream origin\/master<\/pre>\n<p>If you\u2019re cloning a new repo, you\u2019ll just need to make sure that it\u2019s set to \u201cSSH\u201d in the future, and that the URI is configured as <code>git@github.com<\/code>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14750\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/413db8db.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"588\" height=\"367\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_a_Different_SSH_Key\"><\/span>Using a Different SSH Key<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you have multiple SSH keys you need to use though, things can get complicated, which is why Github recommends passwords to newcomers. Say you clone the repo on your desktop, but then want to work from your laptop. You\u2019d have to either add a new key to your Github account, or transfer the key to the laptop.<\/p>\n<p>If you can, you should add a new key. Github supports multiple of them for a reason, and you can give them different names to organize them. Sometimes though, you\u2019ll only have one key, and need to fix things on the client side.<\/p>\n<p>If you just want to use the same key, it\u2019s fine to transfer <code>id_rsa<\/code>\u00a0and <code>id_rsa.pub<\/code>\u00a0to the new machine. But, if that machine already has its own SSH key, you\u2019ll need to use multiple keys.<\/p>\n<p>You can do that by editing SSH\u2019s hosts config file:<\/p>\n<pre>nano ~\/.ssh\/config<\/pre>\n<p>Add two blocks with different names. In this use case, it\u2019s setting up different keys for a personal account vs. a company account.<\/p>\n<pre>Host personal&#13;\n  Hostname github.com&#13;\n  IdentityFile ~\/.ssh\/githubpersonal&#13;\n  IdentitiesOnly yes&#13;\n&#13;\nHost work&#13;\n  Hostname github.com&#13;\n  IdentityFile ~\/.ssh\/githubwork&#13;\n  IdentitiesOnly yes<\/pre>\n<p>You will need two keys named <code>githubpersonal.pub<\/code> \u00a0and <code>githubwork.pub<\/code>, or whatever names you choose to give them. Lastly, you\u2019ll need to remove the remote again and add it back, specifying the name of the block in the host configuration file (which may not match the key name):<\/p>\n<pre>git remote rm origin&#13;\ngit remote add origin git@personal:username\/repository.git<\/pre>\n<p>In this command, \u201cpersonal\u201d is replacing the hostname, <code>github.com<\/code>, in the URL. The reason this is necessary is because SSH\u2019s config defaults to choosing a key based on hostname, which in both the <code>personal<\/code> and <code>work<\/code> blocks is just <code>github.com<\/code>. You must manually specify so Git can select the proper one.\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\/14747\/how-to-switch-a-github-repository-to-ssh-authentication\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Switch a Github Repository to SSH Authentication \u2013 CloudSavvy IT&#8221; Github provides two ways to authenticate with your repository\u2014over HTTPS with a password, and over SSH using private keys. While both work fine, if you want to switch, you\u2019ll need to configure your local repo to use the new credentials. Moving To SSH&#8230;<\/p>\n","protected":false},"author":1,"featured_media":368406,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/d6528811.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-368405","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\/368405","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=368405"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/368405\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/368406"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=368405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=368405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=368405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}