labs / tiddlers / content / labs / lab02 / _Labs_02_Merging Topic
We are pretty much finished with the product editor dialog now and therefor we are also finished with the `product_editor` topic branch.  Now we need to merge it back into the master branch.

The following diagram describes the process that we are about to perform:

{{/Labs/02/images/Merging Topic Branches}}

1. Triple check that everything is committed since you have to try really hard to accidentally lose work that it is committed.  Right click the root of the project and commit from there.  There should be no files that need to be committed.

1.  Before merging the topic branch, we need to sync it with the remote master so that it includes any changes made by other team members.

    First we need to update the local master since work that team members want to share will be in the master branch:

    *  Switch to the master branch using the the repository browser.  Right click the local master branch, and then select <<menu "Checkout Revision > Checkout">>.

    * Pull from upstream.

    Now we have the latest code in the local master.

1.  Switch back to the topic branch again.  Right click <<menu "topic/product_editor > Checkout Revision > Checkout">>.

1.  Merge the local master branch.  Right click <<menu "master > Merge Revision">>.  The default settings are fine, so click <<menu "Merge">>.

     Note that merge operations are performed in the current branch.  You need to first checkout the branch (which we did in the previous step) that you want to perform the merge in.

     Your topic branch is now fully up to date and includes all changes that are in both the remote and local master, and should be able to be merged back into master without any problems since everything is based on the most up-to-date code.

1.  You should test your system to ensure that nothing was broken by merging the remote changes.  There is not much for you to do here other than checking that your dialog still looks OK.  If you had automated tests (we will start adding tests in next week's lab) then you would run those tests to check the system at this point.

1.  Switch back to master by checking it out.

     Uh oh, your dialog just vanished from your project.  That is expected --- that work currently only exists in the topic branch since it hasn't yet been merged into the local master yet.

1. Merge the topic branch.  Right click  <<menu "topic/product_editor > Merge Revision > Merge">>.

    Your dialog should be back, since we have merged the topic branch back into master.

1.  Push to upstream to share your completed work with the rest of the team.

1.  Check GitBucket.  You should be able to see your `ProductEditor` class.

1.  The topic branch is fully merged into master now, so we have no need for it anymore.  Delete the local topic branch by right clicking it in <<menu "Branches > Local">> and selecting <<menu "Delete Branch">>.  If a dialog pops up telling you that the branch has not been properly merged then may have messed up the merge (you probably skipped a step somewhere).  Go back and carefully work through steps 2--9 again.

1.  Close the issue since we have completed the associated task.  You can do this from GitBucket.  Open the issue, and click the little up arrow to the right of the <<menu "Comment">> button.  Select <<menu "Close and comment">>, and then click the <<menu "Close and comment">> button.

1. The following is more of an FYI.  You don't have to do anything in this step for this lab.

    Topic branches are temporary branches that can be deleted once they have been merged back into master and generally don't need to be pushed unless you have a good reason to do so.  You might want to push a topic branch to a remote server if you want another team member to take a look at your work (some development workflows require peer review before merging), or you need to use more than one computer (which is common if you need to test the code on a different environment, or you are working from multiple locations).

    To push a branch in NetBeans you use <<menu "Push...">> rather than <<menu "Push to Upstream">>.  You will be asked to choose which branches you want to push, and you can select your topic branch.

    You should delete the remote topic branch once you are finished with it, otherwise you could end up with hundreds of useless branches on your git server.   You can delete remote topic branches in the repository browser by right clicking them under <<menu  "Branches > Remote">> and selecting <<menu "Delete Branch">>.  You have only deleted the local version at this point --- you still need to push the deletion --- use <<menu "Push...">> rather than <<menu "Push to Upstream">>.  You will need to click the <<menu "Enable Deletes">> button before NetBeans will let you select the deleted branch.  You can identify the deleted branch by the `[R]` annotation.

It is up to you whether you want to use topic branches in INFO202.  There is some benefit to using topic branches even in a single-developer scenario --- it allows you to work on multiple issues at the same time (not recommended, but occasionally necessary if you need to fix a bug in another part of the system while still working on an incomplete feature).  You can also use topic branches to work on experimental code that you are not sure will work --- if the experiment doesn't pan out then you delete the topic branch without ever merging it into master (it can be quite cathartic to destroy all evidence of a failed experiment with a single operation).

You do need to be careful though.  If you aren't regularly merging your branches (first, master into your topic branches, and then your completed topic branches into master) then you can make a mess.

If you aren't confident with using Git then you should stick to doing all of your work in the master branch in INFO202.