Branch and Merge with Git

Create Branch

  1. From within your Git™ repository folder, right-click the white space of the Current Folder browser and select Source Control > Manage Branches. In the Manage Branches dialog box, you can view, switch, create, and merge branches.

      Tip   You can inspect information about each commit node. Select a node in the Branch Browser diagram to view the author, date, commit message, and changed files.

    The Branch Browser in this figure shows an example branch history.

  2. Select a source for the new branch. Click a node in the Branch Browser diagram, or enter a unique identifier in the Source text box. You can enter a tag, branch name, or a unique prefix of the SHA1 hash (for example, 73c637 to identify a specific commit). Leave the default to create a branch from the head of the current branch.

  3. Enter a name in the Branch name text box and click Create.

  4. To work on the files on your new branch, switch your project to the branch.

    In the Branches drop-down list, select the branch you want to switch to and click Switch.

  5. Close the Manage Branches dialog box and work on the files on your branch.

For next steps, see Push and Fetch with Git.

Switch Branch

  1. From within your Git repository folder, right-click the white space of the Current Folder browser and select Source Control > Manage Branches.

  2. In the Manage Branches dialog box, in the Branches drop-down list, select the branch you want to and click Switch.

  3. Close the Manage Branches dialog box and work on the files on your branch.

Merge Branches

Before you can merge branches, you must install command-line Git on your system path and register binary files to prevent Git from inserting conflict markers. See Install Command-Line Git Client.

    Tip   After you fetch changes, you must merge. For more information, see Fetch and Merge.

To merge any branches:

  1. From within your Git repository folder, right-click the white space of the Current Folder browser and select Source Control and Manage Branches.

  2. In the Manage Branches dialog box, from the Branches drop-down list, select a branch you want to merge into the current branch, and click Merge.

  3. Close the Manage Branches dialog box and work on the files on your branch.

If the branch merge causes a conflict that Git cannot resolve automatically, an error dialog box reports that automatic merge failed. Resolve the conflicts before proceeding.

    Caution   Do not move or delete files outside of MATLAB® because this can cause errors on merge.

Keep Your Version

  1. To keep your version of the file, right-click the file and select Mark Conflict Resolved.

  2. Click Commit Modified Files to commit your change that marks the conflict resolved.

Compare Branch Versions

If you merge a branch and there is a conflict in a file, Git marks the file as conflicted and does not modify the contents. Right-click the file and select Source Control > View Conflicts. A comparison report opens that shows the differences between the file on your branch and the branch you want to merge into. Decide how to resolve the conflict. See Resolve Source Control Conflicts.

Revert to Head

  1. From within your Git repository folder, right-click the white space of the Current Folder browser and select Source Control > Manage Branches.

  2. In the Manage Branches dialog box, click Revert to Head to remove all local changes.

Delete Branches

  1. In the Manage Branches dialog box under Branch Browser, expand the Branches drop-down list, and select the branch you want to delete.

  2. On the far right, click the down arrow and select Delete Branch.

    Caution   You cannot undo branch deletion.

Related Examples

Was this topic helpful?