{"id":261267,"date":"2021-05-27T17:00:00","date_gmt":"2021-05-27T14:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-git-with-multiple-remote-repositories-cloudsavvy-it\/"},"modified":"2021-05-27T17:00:00","modified_gmt":"2021-05-27T14:00:00","slug":"how-to-use-git-with-multiple-remote-repositories-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-git-with-multiple-remote-repositories-cloudsavvy-it\/","title":{"rendered":"#How To Use git with Multiple Remote Repositories \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-6a332e75a4e81\" 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-6a332e75a4e81\" 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-use-git-with-multiple-remote-repositories-cloudsavvy-it\/#Remotes_Explained\" >Remotes, Explained<\/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-use-git-with-multiple-remote-repositories-cloudsavvy-it\/#Setting_Up_Multiple_Remotes\" >Setting Up Multiple Remotes<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To Use git with Multiple Remote Repositories \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone wp-image-2266 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Git logo\" width=\"700\" height=\"299\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><code>git<\/code>\u00a0can be configured to push and pull from many locations at once, enabling you to store your code on two different platforms while only maintaining one local copy. Here\u2019s how to set it up.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Remotes_Explained\"><\/span>Remotes, Explained<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The \u201cremote\u201d for a branch is a URL from where your local <code>git<\/code>\u00a0repo fetches changes. Your local <code>git<\/code>\u00a0repo is entirely yours\u2014it isn\u2019t affected by other people\u2019s code until they push their commits to the remote. Hopefully, you\u2019re all using the same remote and everything syncs up, but the remote is just an endpoint. You could clone that endpoint and switch to a new remote without much issue.<\/p>\n<p>Whenever you clone a new repository, the default remote is set as \u201corigin.\u201d You can find the remotes for any given <code>git<\/code>\u00a0repo by running:<\/p>\n<pre>git remote -v<\/pre>\n<p>This will probably display the URL of your main repository on GitHub or whatever service you\u2019re using. If you have multiple remotes, they\u2019ll show up here, too.<\/p>\n<p>But just because <code>origin<\/code>\u00a0is the default remote, it doesn\u2019t mean you\u2019re limited to one. Why would you want two remotes? Well, a good use case is AWS\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/aws.amazon.com\/codecommit\/?tag=reviewgeek-20\">CodeCommit<\/a>. It\u2019s a hosted <code>git<\/code> repository, and has many integrations with their EC2 compute platform, enabling for automated code deployments to your servers, directly from source control.<\/p>\n<p>However, CodeCommit is pretty clunky compared to more focused\u00a0<code>git<\/code>\u00a0providers like GitHub, GitLab, and BitBucket, and doesn\u2019t have the same CI\/CD integrations that make those providers great. So, you\u2019re stuck with a dilemma\u2014use CodeCommit as your default <code>git<\/code>\u00a0solution, or build your automated code deployment pipeline yourself.<\/p>\n<p>However, with multiple remotes, you can easily push code to a second repository. Whenever you want to update your servers, you can push the changes from your primary source control to CodeCommit to start the deployment pipeline.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Setting_Up_Multiple_Remotes\"><\/span>Setting Up Multiple Remotes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Using <code>git<\/code>\u00a0this way is actually quite simple. You add remotes in the same way that you would push an existing folder, except instead of adding the \u201corigin\u201d remote, you give it a different name.<\/p>\n<pre>git remote add &lt;name&gt; &lt;url&gt;<\/pre>\n<p>Then, when you want to push to the second remote, add the remote name and branch to your push command:<\/p>\n<pre>git push second master<\/pre>\n<p>Or, switch the default remote using\u00a0<code>--set-upstream<\/code>:<\/p>\n<pre>git push --set-upstream second master<\/pre>\n<p>This is the simplest setup, however, it requires you to either pass the remote name as an argument, or switch the remote every time.<\/p>\n<p>Really, if you\u2019re using a two remote setup, you\u2019ll probably want a better way to manage pushing code to your second remote. The best way to handle this in <code>git<\/code>\u00a0is to create another branch for code pushed to the second upstream, such as deployments to AWS CodeCommit.<\/p>\n<p>You can create a branch with <code>checkout -b<\/code>:<\/p>\n<pre>git checkout -b deployment<\/pre>\n<p>Then, add the deployment remote:<\/p>\n<pre>git remote add deployment &lt;url&gt;<\/pre>\n<p>and fetch the master branch:<\/p>\n<pre>git fetch deployment master<\/pre>\n<p>Then, you can set the upstream for the current branch by running:<\/p>\n<pre>git branch --set-upstream-to=deployment\/master<\/pre>\n<p>You can repeat this process for any number of branches, making it a great method to keep track of multiple remotes. Remember, though, that this is only a local setup, so if you push this branch to your primary repository, others won\u2019t have their copies of the deployment branch configured to use the second remote automatically.<\/p>\n<p>It would be ideal if the second branch is only one way, meaning, you are only pushing code, not pulling new code, otherwise you may run into unexpected conflicts. Other than that, <code>git<\/code>\u00a0works perfectly fine with multiple remotes.\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\/2464\/how-to-use-git-with-multiple-remote-repositories\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Use git with Multiple Remote Repositories \u2013 CloudSavvy IT&#8221; git\u00a0can be configured to push and pull from many locations at once, enabling you to store your code on two different platforms while only maintaining one local copy. Here\u2019s how to set it up. Remotes, Explained The \u201cremote\u201d for a branch is a URL&#8230;<\/p>\n","protected":false},"author":1,"featured_media":261268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-261267","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\/261267","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=261267"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/261267\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/261268"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=261267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=261267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=261267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}