{"id":11973,"date":"2016-05-16T06:00:00","date_gmt":"2016-05-16T13:00:00","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=11973"},"modified":"2016-05-16T12:25:30","modified_gmt":"2016-05-16T19:25:30","slug":"github-how-to-update-your-fork","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/05\/16\/github-how-to-update-your-fork\/","title":{"rendered":"GitHub: How to update your fork"},"content":{"rendered":"<p>Like almost everyone in the DevOps world, I use GitHub, not because I like it, but because everyone uses it. In fact, I dislike it, because it\u2019s confusing by design. Its misleading language excludes users, rather than welcoming them. To use it, you have to forget the actual meaning of words in English, or even their technical meaning, and use them as though they are arbitrary strings.<\/p>\n<p>Worst of all, because most people memorize a command sequence without fully understanding how it works, when something goes wrong, it\u2019s almost impossible to troubleshoot.<\/p>\n<p>Okay, enough ranting.<\/p>\n<p>I just discovered a solution to one of the problems I encounter frequently, so I want to share it with you. My utmost thanks to PowerShell MVP and Principal Engineer &#8211; Community Engineering for Chef, <a href=\"https:\/\/twitter.com\/StevenMurawski\">Steven Murawski<\/a> for showing me the solution and letting me try it by myself (twice) while he was standing nearby in case of disaster. If this solution works for you, tweet your thanks to Steven.<\/p>\n<h1>Your Fork is Outdated<\/h1>\n<p>Here\u2019s the problem. You create a fork (an online copy) of a repository and a clone (a local copy on disk) of your fork. You spend some time examining the files and interpreting the code. Then, when you\u2019re ready to work, you notice the message on your fork.<\/p>\n<p>&#8220;This branch is <i>n<\/i> commits behind (source-repo).&#8221;<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/05\/clip_image001-1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 30px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image001\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/05\/clip_image001_thumb-1.png\" alt=\"clip_image001\" width=\"380\" height=\"231\" border=\"0\" \/><\/a><\/p>\n<p>That makes sense. While you were working, other people were working, too. So, you want to synchronize your branch of your fork (the online copy) and its clone (the disk copy) with the files in the source repository.<\/p>\n<p>This happens so often and it&#8217;s so predictable that you&#8217;d think there would be a Sync button or something like it, but there&#8217;s not.<\/p>\n<p>Also, if you try something rational, like opening your Git Shell (I use Git for Windows with posh-git) and trying to fetch\/merge or pull (or push or anything else you can think of), that won&#8217;t work, because, by default, those commands synchronize your clone (on disk) with your fork (online) and they&#8217;re already in sync. You need to synchronize your fork\/clone with their source.<\/p>\n<h1>Remote: A link to the Source<\/h1>\n<p>One solution, the one I had been using, is to copy any files you&#8217;ve changed to a different directory, delete your fork, delete your clone, then re-fork and re-clone, and move the changed files back in. Yes, I&#8217;ve done this. Whatever it takes.<\/p>\n<p>The correct Steven-Murawski-approved solution is to create a <b><i>remote<\/i><\/b>, that is, a named link to the source repository, in your clone (on disk). This lets you get and send things from your clone to the source repo.<\/p>\n<p>A remote is just another name, alias, or nickname for the URL to an online repository. In any command that you use the name of the remote, you can use the URL, and vice versa. <\/p>\n<p>Actually, you already have one remote; a remote to the fork, although you might not have noticed. When you clone (make a disk copy) an online repo, the clone command automatically creates a remote to the repository that you cloned, typically your fork. That remote is named <b>origin<\/b>.<\/p>\n<p>To see your remotes, use <b>git remote<\/b>.<\/p>\n<pre class=\"output\">C:\\Github\\dbatools [master]&gt; git remote\r\norigin\r\n<\/pre>\n<p>To see the URL of the remote, add -v (for verbose). These are remotes to my fork, so they have my username, juneb, in the URL.<\/p>\n<pre class=\"output\">C:\\Github\\dbatools [master]&gt; git remote -v\r\norigin https:\/\/github.com\/juneb\/dbatools.git (fetch)\r\norigin https:\/\/github.com\/juneb\/dbatools.git (push)\r\n<\/pre>\n<p>By default, when you use <b>git fetch<\/b>, <b>git merge<\/b>, <b>git pull<\/b>, or <b>git push<\/b>, git uses the <b>origin<\/b> remote. As a result, it gets stuff from or sends stuff to the fork that you cloned.<\/p>\n<p>So, to get or send to the source repo, you need another remote.<\/p>\n<h1>Update your clone and fork<\/h1>\n<p>To get all changed files from a source repository and copy them to the clone, and then to the fork.<\/p>\n<ol>\n<li>On GitHub.com, on the page for the source repository (not your fork), copy the URL of the source repo. It&#8217;s the same URL that you would use to clone the source repo.\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/05\/clip_image003-1.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image003\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/05\/clip_image003_thumb-1.jpg\" alt=\"clip_image003\" width=\"568\" height=\"156\" border=\"0\" \/><\/a><br \/>\n&nbsp;\n<\/li>\n<li>In your shell, navigate to the directory with the clone, and use the following command to create a remote to the source repo.\n<\/p>\n<pre class=\"output\">git remote add &lt;name&gt; &lt;copiedURL&gt;<\/pre>\n<p>For example:<\/p>\n<pre class=\"output\">git remote add upstream https:\/\/github.com\/ctrlbold\/dbatools.git<\/pre>\n<p>The name is arbitrary. You can use any valid string for the name of your remote. The traditional name of a remote to the source repository is <b>&#8216;upstream&#8217;<\/b> which makes about as much sense as the rest of the words in GitHub. In this use, it is a proper noun, not a direction. Regardless, I use it, because people recognize it.\n<\/li>\n<li>Get (fetch) and merge changes from the source repo to your clone. All names, including the remote name and branch name, are case-sensitive.\n<\/p>\n<pre class=\"output\">git fetch &lt;nameOfRemote&gt;\r\ngit merge &lt;nameOfRemote&gt;\\&lt;branch&gt;<\/pre>\n<p>For example,<\/p>\n<pre class=\"output\">git fetch upstream\r\ngit merge upstream\\master<\/pre>\n<\/li>\n<li>Send the changes from your clone (on disk) to your fork (online). You don&#8217;t need to qualify this command with a remote, because the &#8220;origin&#8221; remote is the default.\n<\/p>\n<pre class=\"output\">git push<\/pre>\n<\/li>\n<\/ol>\n<p>Now, your clone and fork have the same content:<\/p>\n<pre class=\"output\">C:\\Github\\dbatools [master]&gt; git status\r\nOn branch master\r\nYour branch is up-to-date with 'origin\/master'.\r\n\r\nnothing to commit, working directory clean\r\n<\/pre>\n<p>And, miraculously, your branch is &#8220;even with master&#8221;.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/05\/clip_image004.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 30px; display: inline; padding-right: 0px; border: 0px;\" title=\"clip_image004\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/05\/clip_image004_thumb.png\" alt=\"clip_image004\" width=\"361\" height=\"193\" border=\"0\" \/><\/a><\/p>\n<p>Thanks, Steven! It works!<\/p>\n<h3>To sync again<\/h3>\n<p>If you continue to work on this project and your fork becomes outdated again, you already have a remote to the source repo. So, just repeat the fetch, merge, and push. There&#8217;s no need to recreate or update the link to the remote repo.<\/p>\n<pre class=\"output\">git fetch upstream\r\ngit merge upstream\\master\r\ngit push<\/pre>\n<p>Not too tough. Having this solution makes my GitHub live a lot easier. I still don&#8217;t like it, but I use it.<\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at <\/em><em><a href=\"mailto:juneb@sapien.com,\">juneb@sapien.com,<\/a><\/em><em> follow her on Twitter at <\/em><a href=\"https:\/\/www.twitter.com\/juneb_get_help\"><em>@juneb_get_help<\/em><\/a><i>, and find her GitHub repos at <a href=\"https:\/\/github.com\/juneb\">https:\/\/github.com\/juneb<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like almost everyone in the DevOps world, I use GitHub, not because I like it, but because everyone uses it. In fact, I dislike it, because it\u2019s confusing by design. Its misleading language excludes users, rather than welcoming them. To use it, you have to forget the actual meaning of words in English, or even [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[932,1107,1108,283],"tags":[786,896,934],"class_list":["post-11973","post","type-post","status-publish","format-standard","hentry","category-beginners","category-git","category-github","category-howto","tag-git","tag-github","tag-juneb"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=11973"}],"version-history":[{"count":24,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11973\/revisions"}],"predecessor-version":[{"id":12055,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11973\/revisions\/12055"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=11973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=11973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=11973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}