{"id":5653,"date":"2016-06-03T11:56:06","date_gmt":"2016-06-03T16:56:06","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=5653"},"modified":"2016-06-06T11:06:11","modified_gmt":"2016-06-06T16:06:11","slug":"three-way-model-merge-and-git","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2016\/06\/03\/three-way-model-merge-and-git\/","title":{"rendered":"Three-Way Model Merge and Git"},"content":{"rendered":"<p>In R2016a, a new <a title=\"https:\/\/www.mathworks.com\/help\/rptgenext\/ug\/merge-simulink-models-from-the-comparison-report.html (link no longer works)\">Three-Way Model Merge<\/a> functionality got introduced. You can find a clear description of this feature in the following documentation pages:<\/p>\n<ul>\n<li><a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2017a\/rptgenext\/examples\/resolve-conflicts-with-simulink-three-way-merge.html (link no longer works)\">Resolve Conflicts with Simulink Three-Way Merge<\/a><\/li>\n<li><a title=\"https:\/\/www.mathworks.com\/help\/rptgenext\/ug\/merge-simulink-models-from-the-comparison-report.html (link no longer works)\">Merge Simulink Models from the Comparison Report<\/a><\/li>\n<\/ul>\n<p>In those documentation pages, the workflow always begins with <em>right-click on the conflicted model file and select <strong>View Conflicts<\/strong><\/em>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/conflictedfile.png\" alt=\"Conflicted file\" \/><\/p>\n<p>In this blog post, I will try to provide a bit of additional information to complement the workflow described in the above links.<\/p>\n<p><strong>Creating a Conflict<\/strong><\/p>\n<p>I created a simple project under Git source control. I could have used <a href=\"https:\/\/github.com\/\">GitHub<\/a>, but I decided to use a Git server we have at MathWorks. At the Git command-line, I cloned two identical repositories:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/gitClone.png\" alt=\"Git Clone\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/TwoProjects.png\" alt=\"two Projects\" \/><\/p>\n<p>In the first repository, I make some modifications to the model. I go in the Modified Files view of the Simulink Project, commit the modified files and push the changes to the remote repository<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/CommitPush.png\" alt=\"commit and Push\" \/><\/p>\n<p>I close the first project, navigate to the second repository, open the Simulink Project there, and modify the model in a different way.<\/p>\n<p><strong>Resolving the Conflict<\/strong><\/p>\n<p>Before trying to commit and push changes as done in the first repository, it is always a good idea to click the <strong>Fetch<\/strong> button to get the latest from the remote repository. Once this is done, you can see if the remote master branch has new submissions. If it does, you want to merge with it before pushing your changes.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/Merge.png\" alt=\"Merge\" \/><\/p>\n<p>Because of the conflict, you will receive this error:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/mergeError.png\" alt=\"Merge Error\" \/><\/p>\n<p>If you go back to Simulink Project, the conflicting files will look like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/ConflictFile.png\" alt=\"Git Conflict\" \/><\/p>\n<p>Right click on the file and select <strong>View Conflicts<\/strong> to launch the Three-Way Model Merge tool. You will then be able to see:<\/p>\n<ul>\n<li>The original model<\/li>\n<li>The latest model in repository 1<\/li>\n<li>The latest model in repository 2<\/li>\n<li>A target model automatically generated by Simulink, attempting to do its best guess at merging the three previous models.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/mergeTool.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/mergeTool_small.png\" alt=\"Three-Way Model Merge\" \/><\/a><\/p>\n<p>In the bottom left section of the Three -Way Model Merge, for each block and signal, you can select which version you want to be merged in the target model. For conflicts that cannot be automatically merged, you can manually fix them in the target model and individually mark them as resolved.<\/p>\n<p>Since the versions from repository 1, repository 2 and the target model can all be opened at the same time, this makes the manual resolution of conflicts quite easy.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/targetMerge.png\" alt=\"Zoom on model merge\" \/><\/p>\n<p>Once you are satisfied with the target model, click the <strong>Accept & Close<\/strong> button:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/acceptClose.png\" alt=\"Accept and Close\" \/><\/p>\n<p>You will then be able to Commit the modified files and push the changes to the repository. If you click the Manage Branches button, you should see how the project got branched and merged back.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/branchEvolution.png\" alt=\"branch evolution\" \/><\/p>\n<p><strong>One more tip...<\/strong><\/p>\n<p>In most cases, this kind of merging challenges happen within the context of a project under source control. However if you just want to launch the tool without any project or source control involved, you can use the following syntax: <\/p>\n<p><tt>slxmlcomp.slMerge(baseFile, mineFile, theirsFile, targetFile);<\/tt><\/p>\n<p>where the 4 inputs are four model files.<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>How do you manage branching? Give a try at the Three-Way Model Merge and let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/simulink\/?p=5653&#comment\">comment here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2016Q2\/branchEvolution.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>In R2016a, a new Three-Way Model Merge functionality got introduced. You can find a clear description of this feature in the following documentation pages:<\/p>\n<p>Resolve Conflicts with Simulink Three-Way... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2016\/06\/03\/three-way-model-merge-and-git\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,16],"tags":[376,378,473],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5653"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=5653"}],"version-history":[{"count":29,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5653\/revisions"}],"predecessor-version":[{"id":5692,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/5653\/revisions\/5692"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}