{"id":10801,"date":"2005-11-13T10:54:00","date_gmt":"2005-11-13T10:54:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/bharry\/2005\/11\/13\/a-branching-and-merging-primer\/"},"modified":"2018-08-14T00:35:09","modified_gmt":"2018-08-14T00:35:09","slug":"a-branching-and-merging-primer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/bharry\/a-branching-and-merging-primer\/","title":{"rendered":"A Branching and Merging Primer"},"content":{"rendered":"<p><font face=\"Arial\">Hmm, OK I guess I jumped too quickly into using unfamiliar terminology.&nbsp; Let me step back and define some of the concepts\/terms a little more and then hopefully that last post will make more sense.<\/font>\n<font face=\"Arial\" size=\"4\"><strong>The Source Tree<\/strong><\/font>\n<font face=\"Arial\">Let&rsquo;s start with what the source tree in the Developer Division looks like.&nbsp; It has the following top level folders (not a complete list but a relevant subset).&nbsp; Each of these folders has its own subtree of subfolders and files.<\/font>\n<font face=\"Arial\">CSharp<br \/>DDSuites<br \/>Public<br \/>Tools<br \/>VB<br \/>VC<br \/>VSCommon<br \/>VSET<\/font>\n<font face=\"Arial\"><strong>CSharp<\/strong>&nbsp;&#8211; is a tree that contains all of the code for the CSharp compiler, project system and related components.<br \/><strong>DDSuites<\/strong>&nbsp;&#8211; is a very large tree of tests for all of the components in the system.&nbsp; Any developer can get this folder and run the tests.&nbsp; This is where we put our unit tests.<br \/><strong>Public<\/strong>&nbsp;&#8211; This is all of the .h files, import libraries, .NET Framework assemblies, etc. that are needed to build.&nbsp; For example we check in the .NET Framework assemblies, Windows SDK, etc.&nbsp; Source code elsewhere in the tree references standard assemblies and header files from this directory.<br \/><strong>Tools<\/strong>&nbsp;&#8211; A big tree that contains all of our compilers, linkers, source control tools, build configuration files, etc.&nbsp; Basically everything needed to build the system.&nbsp; It does not include the IDE &ndash; just command line tools.<br \/><strong>VB<\/strong>&nbsp;&#8211; All the source code that the VB team has written.<br \/><strong>VC<\/strong>&nbsp;&#8211; All of the source code that the VC team has written.<br \/><strong>VSCommon<\/strong>&nbsp;&#8211; A set of shared utility library source code, midl files and the like that are shared across many components in VS.<br \/><strong>VSET<\/strong>&nbsp;&#8211; The source code for the Team System tools.<\/font>\n<font face=\"Arial\">And of course there&rsquo;s more &ndash; probably 30 or 40 top level folders in our tree but this is a good representative sample.<\/font>\n<font face=\"Arial\">Why do we check in all of our tools and includes?&nbsp; Developers already have VS installed on their machine, right?&nbsp; Well, yes but there are several advantages.&nbsp; First by versioning them with the source code we can ensure that we always have a consistent set.&nbsp; If we need to go back and reconstruct a build from 6 months ago we also have the tools we used to build it at that time.&nbsp; One thing to keep in mind is that we are building the tools too so every few months we check in a new version of the compilers and libraries, etc.&nbsp; Using the version control system is a great way to distribute the tools to everyone.&nbsp; Another benefit of doing it this way is that the system is self contained.&nbsp; You can walk up to a newly installed machine (just the OS), create a Team Foundation workspace, do a get, build and everything works.<\/font>\n<br \/><font face=\"Arial\" size=\"4\"><strong>How a Developer Uses the Tree<\/strong><\/font>\n<font face=\"Arial\">I create a workspace on my machine (workspace is a mapping construct that describes what folders to get and where to put them).&nbsp; Very few developers put the entire tree in their workspace because it is so big.&nbsp; Pretty much everyone includes VSCommon, Public and Tools.&nbsp; Beyond that developers include the folders they need.<\/font>\n<font face=\"Arial\">To give an example (from Team Foundation &ndash; which I enlist in), my workspace looks something like this.<\/font>\n<font face=\"Arial\">$\/Main\/tools -&gt; d:\\dd\\tools<br \/>$\/Main\/public -&gt; d:\\dd\\public.<br \/>$\/Main\/vscommon -&gt; d:\\dd\\vscommon<br \/>$\/Main\/ddsuites -&gt; d:\\dd\\ddsuites<br \/>$\/Main\/vset -&gt; d:\\dd\\vset<\/font>\n<font face=\"Arial\">One of the nice things about our build system is that it allows me to build at any level in the tree.&nbsp; I can build everything in my workspace by going to d:\\dd and typing &#8220;build&#8221;.&nbsp; Or I can build just the Team System components by going to d:\\dd\\vset and typing build &ndash; or just Version control from d:\\dd\\vset\\scm\\SourceControl, etc.<\/font>\n<font face=\"Arial\">After I build, all my built binaries end up in d:\\binaries.&lt;cpu&gt;&lt;build type&gt;.&nbsp; For example if I build x86 debug then they end up in d:\\binaries.x86dbg.&nbsp; If I build retail they end up in d:\\binaries.x86fre (don&rsquo;t ask me why retail is called fre :)).<\/font>\n<br \/><font face=\"Arial\" size=\"4\"><strong>On to Branching and Merging<\/strong><\/font>\n<font face=\"Arial\">OK, hopefully with a little background on what the &#8220;tree&#8221; is, the branching part will be a little easier to understand.&nbsp; What I&rsquo;ve described above is how it would all work if all of the developers worked together on the same source at the same time.&nbsp; As I described in my last blog post, this is impractical &ndash; too many developers changing things.<\/font>\n<font face=\"Arial\">So we created branches off of Main and, in fact, no developer actually works in Main &ndash; they all work in some branch.&nbsp; So start at the beginning.&nbsp; We checked all of our source into the tree under $\/Main.&nbsp; We then created branches of main.&nbsp; Using the Source Control Explorer in Team Foundation you can do this by selecting $\/Main and choosing File -&gt; Source Control -&gt; Branch.&nbsp; When the dialog comes up we&rsquo;d choose $\/Lab21.<\/font>\n<font face=\"Arial\">This would create a whole new copy of the source tree that would look something like:<\/font>\n<font face=\"Arial\">$\/Main<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSharp<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DDSuites<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Public<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tools<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VB<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VC<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSCommon<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSET<br \/>$\/Lab21<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSharp<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DDSuites<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Public<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tools<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VB<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VC<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSCommon<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSET<\/font>\n<br \/><font face=\"Arial\">Right after the branch, $\/Main and $\/Lab21 are basically exact copies.&nbsp; Fortunately, however, it doesn&rsquo;t double your disk space usage.&nbsp; The new branch ($\/Lab21) references the same copies of the files that the original, or Parent branch ($\/Main) contains.&nbsp; Only when you actually modify (checkin) a file in one of the branches is another copy of the modified file made.<\/font>\n<font face=\"Arial\">Let&rsquo;s talk for a second about Branch lineage or Parenting.&nbsp; It&rsquo;s very complicated and takes a while to internalize.&nbsp; Let&rsquo;s take a file in the system as an example.<\/font>\n<font face=\"Arial\">There is a file (you can probably guess what it is :))<\/font>\n<font face=\"Arial\">$\/Main\/VSET\/SCM\/SourceControl\/CommandLine\/CommandCheckin.cs<\/font>\n<font face=\"Arial\">When we branched the $\/Main folder above a whole copy of the tree was created.&nbsp; So there is now another &#8220;copy&#8221; (remember we don&rsquo;t actually duplicate the contents until you change it but looking at the tree you can&rsquo;t tell the difference) of CommandCheckin.cs at:<\/font>\n<font face=\"Arial\">$\/Lab21\/VSET\/SCM\/SourceControl\/CommandLine\/CommandCheckin.cs<\/font>\n<font face=\"Arial\">From the perspective of the &ldquo;folder hierarchy&rdquo; the two files are in very different parts of the tree &ndash; one is deep down under $\/Main and the other is deep down under $\/Lab21.<\/font>\n<font face=\"Arial\">However when speaking from a &#8220;branch hierarchy&#8221; perspective rather than a &#8220;folder hierarchy&#8221; perspective $\/Main\/VSET\/SCM\/SourceControl\/CommandLine\/CommandCheckin.cs is the parent of $\/Lab21\/VSET\/SCM\/SourceControl\/CommandLine\/CommandCheckin.cs.&nbsp; When the Lab21 tree was created each file was branched from the corresponding file in the Main tree and this relationship is maintained.&nbsp; Having this relationship allows changes in CommandCheckin.cs to be easily merged back and forth between the two branches.&nbsp; So, imagine I had a change to $\/Lab21\/VSET\/SCM\/SourceControl\/CommandLine\/CommandCheckin.cs that I want to move to the main branch.&nbsp; I can do this by (again using the Source Control Explorer) selecting CommandCheckin.cs under $\/Lab21 and choosing File -&gt; Source Control -&gt; Merge.&nbsp; This will give me a choice of files to merge with and one of them will be the CommandCheckin.cs under $\/Main.&nbsp; After you hit OK, the changes to CommandCheckin.cs under $\/Lab21 will be incorporated into the CommandCheckin.cs under $\/Main.<\/font>\n<font face=\"Arial\">To make managing branches easier, you can do this merging at a higher level too.&nbsp; For example, using the Source Control Explorer as above, I can select the $\/Lab21 folder itself rather than the CommandCheckin.cs file way down below it.&nbsp; When picking the merge target, I pick the $\/Main folder.&nbsp; Because it tracks all of the relationships, it looks down the tree and finds that CommandCheckin.cs has been changed in the $\/Lab21 tree and merges it with the corresponding CommandCheckin.cs in the $\/Main tree.&nbsp; Being able to do this makes managing merging changes between branches dramatically easier.<\/font>\n<font face=\"Arial\">Because changes can be merged in either direction and it&rsquo;s confusing which one you mean.&nbsp; If I say I&rsquo;m merging Lab21 and Main, what do I mean?&nbsp; In order to do this we coined some terminology to indicate the direction of the merge.&nbsp; A &#8220;Reverse Integration&#8221; (abbreviated RI) is a merge from the &#8220;Branch child&#8221; to the &#8220;Branch parent&#8221;. And a &#8220;Forward Integration&#8221; (abbreviated FI) is a merge from the &#8220;Branch parent&#8221; into a &#8220;Branch child&#8221;.&nbsp; So using the example from above, if I said I&rsquo;m going to RI Lab21.&nbsp; We know that means we are going to merge changes that have been made to files under $\/Lab21 into the corresponding files under $\/Main.<\/font>\n<font face=\"Arial\">Hopefully that helps understand the difference between the folder hierarchy and the branch hierarchy.&nbsp; We can talk meaningfully about both.&nbsp; When we do this I represent them as follows:<\/font>\n<font face=\"Arial\">Repeating the folder hierarchy from above:<\/font>\n<font face=\"Arial\">$\/Main<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSharp<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DDSuites<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Public<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tools<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VB<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VC<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSCommon<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSET<br \/>$\/Lab21<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSharp<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DDSuites<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Public<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tools<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VB<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VC<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSCommon<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VSET<\/font>\n<font face=\"Arial\">However, I&rsquo;d represent the Branch Hierarchy as follows:<\/font>\n<font face=\"Arial\">Main<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lab21<\/font>\n<font face=\"Arial\">What this means is that Lab21 was created by branching from Main.&nbsp; Even though in the folder hierarchy they are peers, in the branch hierarchy Lab21 is a &#8220;child&#8221; of Main.&nbsp; All of the files in Lab21 were branched from the corresponding files in Main.&nbsp; So looking at a more complex example from my incomprehensible blog post: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p;&nbsp; &hellip;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RTM<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Servicing<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VSTFRTM<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &hellip;<\/font>\n<font face=\"Arial\">This says that Lab21, Lab22, Lab23 and RTM were created by branching from Main.&nbsp; Lab21dev was created by branching from Lab21.&nbsp; Clr was created by branching from Lab21dev and so forth.&nbsp; When it gets this complicated it becomes even more useful to be able to talk about RI&rsquo;s and FI&rsquo;s.&nbsp; For example, I RI changes from Clr into main (done by merging Clr into Lab21dev, then merging Lab21dev into Lab21, then merging Lab21 into Main).&nbsp; An I FI Main into TeamFoundation (done by merging Main into Lab23, then Lab23 into Lab23dev and finally Lab23dev into TeamFoundation).<\/font>\n<br \/><font face=\"Arial\" size=\"4\"><strong>Closing<\/strong><\/font>\n<font face=\"Arial\">I hope that&rsquo;s enough background to understand the previous blog post.&nbsp; That post is more about the end result and the rationale behind it than the mechanics and concepts behind it.&nbsp; I hope this has enough of those to make the former comprehensible.&nbsp; If not, let me know and I&rsquo;ll try again &#61514;<\/font><\/p>\n<p><font face=\"Arial\">Brian<br \/><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hmm, OK I guess I jumped too quickly into using unfamiliar terminology.&nbsp; Let me step back and define some of the concepts\/terms a little more and then hopefully that last post will make more sense. The Source Tree Let&rsquo;s start with what the source tree in the Developer Division looks like.&nbsp; It has the following [&hellip;]<\/p>\n","protected":false},"author":244,"featured_media":14617,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"acf":[],"blog_post_summary":"<p>Hmm, OK I guess I jumped too quickly into using unfamiliar terminology.&nbsp; Let me step back and define some of the concepts\/terms a little more and then hopefully that last post will make more sense. The Source Tree Let&rsquo;s start with what the source tree in the Developer Division looks like.&nbsp; It has the following [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts\/10801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/users\/244"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/comments?post=10801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts\/10801\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/media\/14617"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/media?parent=10801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/categories?post=10801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/tags?post=10801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}