{"id":296,"date":"2020-01-09T12:01:44","date_gmt":"2020-01-09T06:31:44","guid":{"rendered":"https:\/\/openapex.org\/spaces\/software\/?p=296"},"modified":"2022-10-12T18:07:29","modified_gmt":"2022-10-12T12:37:29","slug":"git-working-with-multiple-github-accounts","status":"publish","type":"post","link":"https:\/\/mutesoft.com\/spaces\/software\/git-working-with-multiple-github-accounts.html","title":{"rendered":"Git &#8211; Working with Multiple GitHub Accounts"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/02\/git.png\" alt=\"Git\" class=\"wp-image-1053\" width=\"910\" height=\"380\"\/><figcaption>Git<\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><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:\/\/mutesoft.com\/spaces\/software\/git-working-with-multiple-github-accounts.html\/#1_Context\" >1. Context<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-working-with-multiple-github-accounts.html\/#2_Generate_and_Configure_SSH_Keys\" >2. Generate and Configure SSH Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-working-with-multiple-github-accounts.html\/#3_Create_Aliases_and_Configure\" >3. Create Aliases and Configure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-working-with-multiple-github-accounts.html\/#4_Troubleshooting\" >4. Troubleshooting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/mutesoft.com\/spaces\/software\/git-working-with-multiple-github-accounts.html\/#5_Conclusion\" >5. Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1-context\"><span class=\"ez-toc-section\" id=\"1_Context\"><\/span><strong>1. Context<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Do you use two or more GitHub accounts &#8211; one for official use, another for personal use, and so on? Did you encounter issues like Git commands are using the wrong SSH key or the wrong GitHub user id or you can use repositories from one GitHub account but not from others? If yes, then read on.<\/p>\n\n\n\n<p>In this article, we are assuming you have two GitHub accounts <code>account1<\/code> and <code>account2<\/code> setup using emails Ids <code>account1@gmail.com<\/code> and <code>account2@gmail.com<\/code> respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-generate-and-configure-ssh-keys\"><span class=\"ez-toc-section\" id=\"2_Generate_and_Configure_SSH_Keys\"><\/span><strong>2. Generate and Configure SSH Keys<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For each of your GitHub accounts, generate SSH keys using the corresponding email Ids and add the public key to GitHub.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Generate keys for the first account. Default file name is id_rsa, change it to github_account1_id_rsa\n# It creates two files: ~\/.ssh\/github_account1_id_rsa and ~\/.ssh\/github_account1_id_rsa.pub\nssh-keygen -t rsa -b 4096 -C \"account1@gmail.com\"\n\n\n# Generate keys for the second account. Default file name is id_rsa, change it to github_account2_id_rsa\n# It creates two files: ~\/.ssh\/github_account2_id_rsa and ~\/.ssh\/github_account2_id_rsa.pub\nssh-keygen -t rsa -b 4096 -C \"account2@gmail.com\"<\/pre>\n\n\n\n<p>Copy the above generated public key (the content of <code>.pub<\/code> file) and add it to the corresponding GitHub account at <code>Settings &gt; SSH and GPG Keys &gt; Add New<\/code> in <a href=\"https:\/\/github.com\/settings\/ssh\/new\">https:\/\/github.com\/settings\/ssh\/new<\/a> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"605\" src=\"https:\/\/openapex.org\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image.png\" alt=\"\" class=\"wp-image-300\" srcset=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image.png 1010w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-300x180.png 300w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2020\/01\/image-768x460.png 768w\" sizes=\"auto, (max-width: 1010px) 100vw, 1010px\" \/><figcaption>Add SSH Key to GitHub account<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-create-aliases-and-configure\"><span class=\"ez-toc-section\" id=\"3_Create_Aliases_and_Configure\"><\/span><strong>3. Create Aliases and Configure<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Create two different aliases of GitHub hosts with the appropriate identity file in <code>~\/.ssh\/config<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Alias for the first GitHub account\nHost github.com-account1\n   HostName github.com\n   User git\n   IdentityFile ~\/.ssh\/github_account1_id_rsa\n   IdentitiesOnly yes\n\n# Alias for the second GitHub account\nHost github.com-account2\n   HostName github.com\n   User git\n   IdentityFile ~\/.ssh\/github_account2_id_rsa\n   IdentitiesOnly yes<\/pre>\n\n\n\n<p>Now, change the remote URL of the local Git repo in <code>&lt;repo_path&gt;\/.git\/config<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># In first repo from first account\n[remote \"origin\"]\n\turl = git@github.com-account1:userid1\/repo1.git\n\tfetch = +refs\/heads\/*:refs\/remotes\/origin\/*\n\n# In second repo from second account\n[remote \"origin\"]\n\turl = git@github.com-account2:userid2\/repo2.git\n\tfetch = +refs\/heads\/*:refs\/remotes\/origin\/*<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-troubleshooting\"><span class=\"ez-toc-section\" id=\"4_Troubleshooting\"><\/span><strong>4. Troubleshoot<\/strong>ing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you encounter issues, enable verbose mode to troubleshoot. All subsequent Git commands will produce SSH logs that will help you understand and fix the issue.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Enable verbose in the repo\ngit config core.sshCommand \"ssh -vvv\"\n\n# Disable verbose in the repo\ngit config core.sshCommand \"ssh\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-conclusion\"><span class=\"ez-toc-section\" id=\"5_Conclusion\"><\/span><strong>5. Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You are all set to use multiple GitHub accounts from your machine. This approach is applicable when you are using multiple accounts in GitLab, BitBucket, etc. as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you have messed up a file or made temporary changes locally (e.g. for debugging) and you want to get rid of the local changes, there is an easy way out.<\/p>\n","protected":false},"author":2,"featured_media":300,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"When you have messed up a file or made temporary changes locally (e.g. for debugging) and you want to get rid of the local changes, there is an easy way out.","_seopress_robots_index":"","_vp_format_video_url":"","_vp_image_focal_point":[],"footnotes":""},"categories":[32],"tags":[31],"class_list":["post-296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools","tag-git"],"_links":{"self":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":12,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":1300,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/296\/revisions\/1300"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/media\/300"}],"wp:attachment":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}